0

このアクティビティでリスト ビューが初期化されない理由がわかりません。同じアプリ内の他の 2 つのアクティビティからの同じコード フローで動作します。デバッグすると、listA が常に null であることがわかります。findViewById を新しい ListView() アイテムに置き換えると、コンパイルして実行されます。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
4

1 に答える 1

0

おそらく、displayNotList() を呼び出す前に setContentView(R.layout.some_layout) を呼び出すのを忘れていました。この場合、findViewById は null を返します。

于 2013-10-08T16:36:10.683 に答える