开发者

Problem updating a SQLite Database

I have a list view that is populated from my personal SQLite DB that shows information about an event. I am trying to create a way to "favorite" this event using a checkbox on the event's description page. The code will try to update the database when a user checks the checkbox and then initiates the onPause()

I have looked up some examples of updating rows in a database, but no matter what I try I seem to always get stuck with a NullPointerException.

Let me know if there is any other code that you would need to look at to help me resolve this problem.

Here is the code I call when the user moves away from the description activity to see if they have checked the checkbox:

@Override
protected void onPause() {
    super.onPause();
    DataBaseHelper myDbHelper = new DataBaseHelper(this);
    if (panelCheckBox.isChecked()) {
        int mySchedule = 1;
        myDbHelper.updateRow(mRowId, mySchedule);
    }

And here is the updateRow code in the DataBaseHelper

public void updateRow(long rowId, Integer mySchedule) {
    if(mySchedule != null){
    ContentValues args = new ContentValues();
    args.put("mySchedule", mySchedule);
    myDataBase.update(DATABASE_PANELS_TABLE, args, "_id=" + rowId, null);
    }
}

Here is the error from logcat:

ERROR/AndroidRuntime(689): Uncaught handler: thread main exiting due to uncaught exception

ERROR/AndroidRuntime(689): java.lang.RuntimeException: Unable to pause activity {com.tagrost.AndroidConventionApp/com.tagrost.AndroidConventionApp.PanelDescActivity}: java.lang.NullPointerException

ERROR/AndroidRuntime(689):     at android.app.ActivityThread.perf开发者_运维知识库ormPauseActivity(ActivityThread.java:3162)

ERROR/AndroidRuntime(689):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)

ERROR/AndroidRuntime(689):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)

ERROR/AndroidRuntime(689):     at android.app.ActivityThread.access$2400(ActivityThread.java:119)

ERROR/AndroidRuntime(689):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1874)

ERROR/AndroidRuntime(689):     at android.os.Handler.dispatchMessage(Handler.java:99)  

ERROR/AndroidRuntime(689):     at android.os.Looper.loop(Looper.java:123)

ERROR/AndroidRuntime(689):     at android.app.ActivityThread.main(ActivityThread.java:4363)

ERROR/AndroidRuntime(689):     at java.lang.reflect.Method.invokeNative(Native Method)  

ERROR/AndroidRuntime(689):     at java.lang.reflect.Method.invoke(Method.java:521)

ERROR/AndroidRuntime(689):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860

ERROR/AndroidRuntime(689):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

ERROR/AndroidRuntime(689):     at dalvik.system.NativeStart.main(Native Method)

ERROR/AndroidRuntime(689): Caused by: java.lang.NullPointerException

ERROR/AndroidRuntime(689):     at com.tagrost.AndroidConventionApp.DataBaseHelper.updateRow(DataBaseHelper.java:198)

ERROR/AndroidRuntime(689):     at com.tagrost.AndroidConventionApp.PanelDescActivity.onPause(PanelDescActivity.java:67)  

ERROR/AndroidRuntime(689):     at android.app.Activity.performPause(Activity.java:3782)  

ERROR/AndroidRuntime(689):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)

ERROR/AndroidRuntime(689):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3149)


So the real root cause of your problem appears to be simple: You create a new dbHelper, but you never explicitly open the database. You look to be closely paralleling the standard Android Notepad tutorial. Pull it up and look at how NotesDbAdaptor wraps SQLiteOpenHelper. you need a call to myDbHelper.open() before you can use myDbHelper.

Another key change you should make is to follow that same pattern as shown in Notepad sample. You should keep the db as a member variable -- initialize with new and then Open it during onCreate(). (it might take half a second to open the database. Better to do that once at init than once on every change.) See the example in Notepadv1.java.

Again, this notepad sample looks like a very close parallel to what you're doing. in step 2 of the tutorial they open up a details page to edit a note, just like you're opening a details page on your... whatever it is. Then, step 3 shows exactly how to make it robust in terms of lifecycle events like onPause and onResume. It is tricky the first time you do it. Walk through their process of getting it right step by step, and adapt to your situation and do the same, and you should soon pick it up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜