listView.setAdapter()上的Android nullpointer

时间:2015-01-08 10:06:48

标签: android listview android-listview android-adapter

在我的主要活动中的onCreate方法中,我想将适配器设置为listview,如下所示:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTxt = (EditText) findViewById(R.id.msg);
        btn = (Button) findViewById(R.id.addNom);
        list = (ListView) findViewById(R.id.list_item);
        arrayList = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
        list.setAdapter(adapter);
}

我在

获得了一个nullpointer异常
list.setAdapter() 

因为

后列表为空
list = (ListView) this.findViewById(R.id.list_item);

我假设在任何null对象上调用方法都会导致问题;我怎么能绕过这个?应该用List填充ListView吗?我没有正确实例化ListView对象吗?

这是stacktrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ciltkick.nom/com.example.ciltkick.nom.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.ciltkick.nom.MainActivity.onCreate(MainActivity.java:52)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

这是布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <EditText android:id="@+id/msg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/msg"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/addNom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send"
            android:onClick="addNom"/>
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical">

        <ListView android:id="@+id/list_item"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableRow>

</TableLayout>

2 个答案:

答案 0 :(得分:0)

您的问题是在getApplicationContext中呼叫onCreate,请参阅this question

作为解决方案,您可以这样做:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayList);

答案 1 :(得分:0)

我修复了这个问题,原因是布局中的片段结构不佳;代码很好。谢谢!