0

我的历史列表视图有一些问题。在 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;
}

这是我的日志猫

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 列,分别为 string、string、string、int、string(用于存储 Uri)

在此先感谢您的帮助。

编辑:将 new_item 添加到列表项

4

2 回答 2

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值后,它似乎丢失了。

于 2012-06-28T02:15:00.160 回答
1

您应该使用 aCursorAdapter来显示您的内容Cursor,然后将其绑定到您的ListViewwith setListAdapter

于 2012-06-28T02:30:16.227 回答