Logcat有Null指针异常吗?

时间:2012-06-28 01:56:42

标签: android listview

我的历史列表视图遇到了一些问题。 在logcat上它说明了问题的原因是new_item。

这是我的代码

c.mDbHelper.fetchAllNotes();
    Item new_item;    
for (i=0; i<c.getCount(); i++) {

    new_item.set(c.getString(1), c.getString(2), c.getInt(4), Uri.parse(c.getString(5)));
    item.add(new_item);
    c.moveToNext();
}

项目是我制作的课程。这是set()方法。

public void set(String name, String code, int qty, Uri imageuri){
    this.name = name;
    this.code = code;
    this.qty = qty;
    this.imageuri = imageuri;
}

这是我的logcat

06-28 09:12:49.528: E/AndroidRuntime(818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.avs.QRscanner/com.android.avs.QRscanner.ViewStock}: java.lang.NullPointerException
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.os.Looper.loop(Looper.java:123)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 09:12:49.528: E/AndroidRuntime(818):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 09:12:49.528: E/AndroidRuntime(818):  at java.lang.reflect.Method.invoke(Method.java:507)
06-28 09:12:49.528: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-28 09:12:49.528: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-28 09:12:49.528: E/AndroidRuntime(818):  at dalvik.system.NativeStart.main(Native Method)
06-28 09:12:49.528: E/AndroidRuntime(818): Caused by: java.lang.NullPointerException
06-28 09:12:49.528: E/AndroidRuntime(818):  at     com.android.avs.QRscanner.CustomAdapter.getCount(CustomAdapter.java:24)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.widget.ListView.setAdapter(ListView.java:454)
06-28 09:12:49.528: E/AndroidRuntime(818):  at com.android.avs.QRscanner.ViewStock.onCreate(ViewStock.java:36)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-28 09:12:49.528: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-28 09:12:49.528: E/AndroidRuntime(818):  ... 11 more

DbHelper有5列,它们包含字符串,字符串,字符串,整数,字符串(用于存储Uri)

提前感谢您的帮助。

编辑:将new_item添加到列表项

2 个答案:

答案 0 :(得分:1)

您是否需要初始化new_item,即(第3行)

c.mDbHelper.fetchAllNotes();
Item new_item;  // <-- Add this (thx jack57)
for (i=0; i<c.getCount(); i++) {
 new_item = new Item();    // <-- Initialize here to prevent NullPointerException
 new_item.set(c.getString(1), c.getString(2), c.getInt(4), Uri.parse(c.getString(5)));
 c.moveToNext();
}

您可能想在某处使用Item对象。在循环转到下一个i值后,它似乎丢失了。

答案 1 :(得分:1)

您应使用CursorAdapter显示Cursor的内容,然后使用ListView将其绑定到setListAdapter