listview初始化时出现空指针异常

时间:2013-10-08 16:26:33

标签: android

我似乎无法弄清楚为什么我的列表视图没有在此活动中初始化。它使用来自同一应用程序中其他2个活动的相同代码流。当我调试时,它显示我的listA始终为null。如果我用一个新的ListView()项替换findViewById,它编译并运行。它似乎没有引用xml listview。以下是代码

public void displayNotList()
{

//instantiates list from xml
listA = (ListView) findViewById(R.id.notificationList);

//listA = new ListView(this);

//sets the list to view the name of the food from the database
String [] columns = new String[]
        {

            InventoryActivity.inventoryDb.KEY_NAME, InventoryActivity.inventoryDb.KEY_EXPDATE

        };
//binds array to the list
int[] to = new int[]
        {
        R.id.datenamecolumn, R.id.datecolumn
        };
//interates through the database returning all the movies
cursorS = InventoryActivity.inventoryDb.getAllFoods();

//creates a new listadapter
listAdapterS = new SimpleCursorAdapter(this, R.layout.datelistrow,   cursorS,columns,to, 0 );

//binds the list to the list adapter
listA.setAdapter(listAdapterS);



}

现在是xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
 android:id="@+id/notificationList"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1.0"
 />

</LinearLayout>

日志

10-08 10:09:03.174: E/AndroidRuntime(3955): FATAL EXCEPTION: main
10-08 10:09:03.174: E/AndroidRuntime(3955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foodstorage/com.example.foodstorage.NotificationActivity}: java.lang.NullPointerException
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.view.View.performClick(View.java:4240)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.view.View$PerformClick.run(View.java:17721)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.os.Handler.handleCallback(Handler.java:730)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.os.Looper.loop(Looper.java:137)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at java.lang.reflect.Method.invoke(Method.java:525)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at dalvik.system.NativeStart.main(Native Method)
10-08 10:09:03.174: E/AndroidRuntime(3955): Caused by: java.lang.NullPointerException
10-08 10:09:03.174: E/AndroidRuntime(3955):     at com.example.foodstorage.NotificationActivity.displayNotList(NotificationActivity.java:66)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at com.example.foodstorage.NotificationActivity.onCreate(NotificationActivity.java:32)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.Activity.performCreate(Activity.java:5133)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 10:09:03.174: E/AndroidRuntime(3955):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-08 10:09:03.174: E/AndroidRuntime(3955):     ... 18 more

1 个答案:

答案 0 :(得分:0)

也许你忘了在调用displayNotList()之前调用setContentView(R.layout.some_layout)。如果是这种情况,则findViewById将返回null

相关问题