使用SQLite填充ListView的问题

时间:2011-05-25 08:44:03

标签: android sqlite

我正在尝试将我自己的SQLite数据库填充到android listview中进行查看但有问题,代码运行但会产生“意外退出”。

我已经检查了我的DDMS,模拟器在数据库中有我的SQLite文件,该文件是从资产文件中复制的。

我不确定这里出了什么问题,任何人都可以给我一些指示,我应该从哪里开始寻找?或者我在下面提供的代码有什么问题吗?我可以在哪里检查代码中的潜在扭结?

谢谢!!!

private void fillData() {

    Cursor drugsCursor = mDbHelper.fetchAllDrugs();
    startManagingCursor(drugsCursor);

        //Creating an array to specify the fields we want
    String[] from = new String[] {DrugsDbAdapter.KEY_ROWID};

        //and an array of the fields we want to bind in the view
    int[] to = new int[] {R.id.text1};

        //To Create the simple cursor adapter and set it to display
    SimpleCursorAdapter drugs = new SimpleCursorAdapter(this, R.layout.drug_row, drugsCursor, from, to);

    setListAdapter(drugs);

}

和处理Cursor的DrugsDbAdapter.java在这里

public Cursor fetchAllDrugs() {     
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DRUG, KEY_CONTENT, KEY_INDICATION, KEY_DOSAGE, KEY_SPECIALPRECAUTION} , null, null, null, null, null);    
}

我的LogCat的输出(输出的红色部分 - 我应该发布整个日志吗?)是:

05-25 18:12:22.338: WARN/dalvikvm(27763): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763): FATAL EXCEPTION: main
05-25 18:12:22.434: ERROR/AndroidRuntime(27763): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.medboxsd/com.paad.medboxsd.medboxsd}: java.lang.NullPointerException
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.os.Looper.loop(Looper.java:123)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at java.lang.reflect.Method.invoke(Method.java:521)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at dalvik.system.NativeStart.main(Native Method)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763): Caused by: java.lang.NullPointerException
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at com.paad.medboxsd.medboxsd.fillData(medboxsd.java:67)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at com.paad.medboxsd.medboxsd.onCreate(medboxsd.java:56)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-25 18:12:22.434: ERROR/AndroidRuntime(27763):     ... 11 more
05-25 18:12:22.514: WARN/ActivityManager(59):   Force finishing activity com.paad.medboxsd/.medboxsd
05-25 18:12:23.034: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450fc298 com.paad.medboxsd/.medboxsd}

1 个答案:

答案 0 :(得分:0)

我怀疑问题是您没有提供名为' _id'你的游标。请参阅我对Android: column '_id' does not exist problem

的回答