我似乎无法弄清楚为什么我的列表视图没有在此活动中初始化。它适用于同一应用程序中其他 2 个活动的相同代码流。当我调试时,它显示我的 listA 始终为空。如果我用新的 ListView() 项目替换 findViewById,它会编译并运行。它似乎没有引用 xml 列表视图。下面是代码
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