使用ArrayList创建的ListView给出NullPointerException

时间:2014-05-13 21:48:12

标签: listview android-listview arraylist nullpointerexception

我正在尝试使用我已在系统中填写的arraylist填写我的ListView。

以下是ListActivity中的OnCreate代码:

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

    history = new History(context);

    reportListView = (ListView) findViewById(R.id.reportListView);

    ArrayList<String> al = new ArrayList<String>(); 
    al=history.getHistory(DatabaseAdapter.RUNNER_HISTORY_TABLE).activityDate;
    final ArrayAdapter<String> ad;
    ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,al);
    reportListView.setAdapter(ad); //exception here


}

以下是布局文件的相关xml代码:

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/reportListView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center" >
    </ListView>

</LinearLayout>

这是日志:

 05-13 18:45:51.216: E/AndroidRuntime(19107): FATAL EXCEPTION: main
 05-13 18:45:51.216: E/AndroidRuntime(19107): Process: com.example.jfitnessfunctiontester, PID: 19107
 05-13 18:45:51.216: E/AndroidRuntime(19107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jfitnessfunctiontester/activities.ReportListViewActivity}: java.lang.NullPointerException
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.os.Handler.dispatchMessage(Handler.java:102)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.os.Looper.loop(Looper.java:136)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.ActivityThread.main(ActivityThread.java:5017)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at java.lang.reflect.Method.invokeNative(Native Method)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at java.lang.reflect.Method.invoke(Method.java:515)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at dalvik.system.NativeStart.main(Native Method)
 05-13 18:45:51.216: E/AndroidRuntime(19107): Caused by: java.lang.NullPointerException
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at activities.ReportListViewActivity.onCreate(ReportListViewActivity.java:41)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.Activity.performCreate(Activity.java:5231)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 05-13 18:45:51.216: E/AndroidRuntime(19107):   ... 11 more

谢谢!

1 个答案:

答案 0 :(得分:0)

由于某种原因,我将异常改为“RuntimeException:由于某种原因,你的内容必须有一个其属性为'android.R.id.list'的ListView”

在另一个线程上找到解决方案: RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'