【问题标题】:Android Studio: ArrayAdapter issues with listviewAndroid Studio:列表视图的 ArrayAdapter 问题
【发布时间】:2016-10-19 13:24:45
【问题描述】:

我正在尝试为列表视图使用自定义布局,以及从 res/strings 中提取的字符串数组。起初我尝试使用

adapter = ArrayAdapter.createFromResource(this,R.array.android_versions,R.layout.list_view_custom)

但是收到一条错误消息,您必须为 textview 提供资源 ID,模拟器将在该资源 ID 上关闭。所以我尝试使用 newArrayAdapter,如下面的代码所示,使用第 4 行来获取我的字符串数组。由于在我尝试使用自定义布局之前这一切都有效,我认为问题出在第 4、第 9 和第 10 行。

public class ListViewActivity extends AppCompatActivity {
    ListView listview;
    ArrayAdapter<String> adapter;
    List<String> android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        listview = (ListView)findViewById(R.id.listview);
        adapter = new ArrayAdapter<>(this,R.layout.list_view_custom,R.id.listview_custom,android_versions);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " is selected",Toast.LENGTH_LONG).show();
            }
        });

    }

}

这里是ListViewActivity的布局,content_list_view.,是通过activity_list_view获取的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.andrewtakao.testapp.ListViewActivity"
    tools:showIn="@layout/activity_list_view">
    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ListView>

</RelativeLayout>

这是我试图在 ListView 中实现的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/listview_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:textAppearance="?android:textAppearanceLarge"
        android:text="@string/apple"
        android:gravity="center_horizontal"/>

</LinearLayout>

最后这是我最新的错误信息:

-18 18:51:17.682 12567-12567/com.example.andrewtakao.testapp I/Choreographer:跳过 112 帧!该应用程序可能也在做 在它的主线程上做了很多工作。 10-18 18:55:11.808 12567-12567/com.example.andrewtakao.testapp D/AndroidRuntime:关闭 关闭 VM 10-18 18:55:11.809 12567-12567/com.example.andrewtakao.testapp E/AndroidRuntime:致命异常:主要 进程:com.example.andrewtakao.testapp,PID:12567 java.lang.RuntimeException:无法实例化活动 组件信息{com.example.andrewtakao.testapp/com.example.andrewtakao.testapp.ListViewActivity}: java.lang.NullPointerException:尝试调用虚拟方法 'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在 android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上 在 android.content.ContextWrapper.getResources(ContextWrapper.java:87) 在 android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) 在 android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:551) 在 com.example.andrewtakao.testapp.ListViewActivity.(ListViewActivity.java:20) 在 java.lang.Class.newInstance(本机方法) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1067) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在 android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(Native Method)

谁能解释我做错了什么?非常感谢。

【问题讨论】:

    标签: android listview android-arrayadapter


    【解决方案1】:

    您将空上下文传递给的异常详细信息,我怀疑它在这里:

    List<String> android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions));
    

    onCreate 中移动您的列表实例化。

    List<String> android_versions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions))
        listview = (ListView)findViewById(R.id.listview);
    

    解释:Activity 中使用getResources() 隐含地指代Activity 本身,它本身就是Context。因为在onCreate 被调用之前,Context 尚未实例化,所以Context 将为空。只有在onCreate 被调用后,Context 才可用。

    【讨论】:

    • 太棒了!太感谢了。但你也可以电子
    • 太棒了!非常感谢 - 你能解释一下 null 上下文是在哪里传递的吗?
    • @AndrewTakao 有你的解释。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多