Problems with populating a ListView with SQLite
I'm trying to populate my own SQLite db into the android listview for view开发者_Python百科ing but have a problem, the code runs but will generate an "unexpected quitting".
I've checked my DDMS and the emulator has my SQLite file within the database which was copied from the assets file.
I'm not sure what went wrong here, can anyone can give me some pointers where i should start looking from? Or is there anything even wrong with the code I've provided below ? Any where i can check for potential kinks in the code ?
Thank you !!!
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);
}
and the DrugsDbAdapter.java handling the Cursor is here
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);
}
The output of my LogCat (the red color portion of the output- should i be posting the whole log?) is:
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}
I suspect the problem is that you're not providing a column named '_id' to your Cursor. See my answer to Android: column '_id' does not exist problem
精彩评论