0

I'm trying to populate my own SQLite db into the android listview for viewing 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}
Squonk
  • 48,735
  • 19
  • 103
  • 135
jamen
  • 313
  • 7
  • 16

1 Answers1

0

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

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • I always thought the _id column is only important if I want to handle onClick events. Not for showing data in the list. – Flo May 25 '11 at 09:41
  • @Flo: The cursor MUST have a column called '_id' either returned from an actual DB table column named '_id' or one which is aliased with a raw SQL query. It's what the cursor uses as an index regardless of whether the data is ever displayed, selected or whatever. – Squonk May 25 '11 at 10:21
  • @MrSquonk : Thank you! I have followed the sqlite database setup using my own file following instructions from http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ which is quite thorough. I've set up my sqlite file with _id inside to load ... and i've also defined a KEY_ROWID = "_id" which i use to query but just cannot understand why ... – jamen May 25 '11 at 10:54
  • @jamen: OK, probably not what I was thinking then. So which line is line 67 of 'medboxsd.java'? – Squonk May 25 '11 at 11:04
  • @MrSquonk: Thank you for replying ! line 67 of medboxsd.java is the first line in the first code box "Cursor drugsCursor = mDbHelper.fetchAllDrugs(); " I have been trying to solve this problem for 2 full days ... is it possible that you can have a look for me ? its a code where i'm trying to load and populate a database into the device , i can zip my whole file to send you ? – jamen May 25 '11 at 16:01
  • @MrSquonk: Thanks I've tried to figure out the LogCat output and I've solved the immediate problem :) Thanks !!!! – jamen May 25 '11 at 17:33