简体   繁体   English

更新SQLite数据库时出现问题

[英]Problem updating a SQLite Database

I have a list view that is populated from my personal SQLite DB that shows information about an event. 我有一个从个人SQLite数据库填充的列表视图,该列表视图显示有关事件的信息。 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() 当用户选中复选框然后启动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. 我查看了一些更新数据库中行的示例,但是无论我尝试什么,我似乎总是被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 这是DataBaseHelper中的updateRow代码

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: 这是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.performPauseActivity(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. 因此,导致问题的真正根本原因似乎很简单:您创建了一个新的dbHelper,但从未明确打开数据库。 You look to be closely paralleling the standard Android Notepad tutorial . 您看起来与标准的Android记事本教程非常相似。 Pull it up and look at how NotesDbAdaptor wraps SQLiteOpenHelper . 拉起它,看看NotesDbAdaptor如何包装SQLiteOpenHelper you need a call to myDbHelper.open() before you can use myDbHelper. 您需要先调用myDbHelper.open()才能使用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(). 您应该将db保留为成员变量-使用new初始化,然后在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. (打开数据库可能需要半秒钟。在初始化时执行一次操作比在每次更改时执行一次操作要好。)请参见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. 在教程的第2步中,他们打开了一个详细信息页面来编辑注释,就像您在...上打开一个详细信息页面一样。 Then, step 3 shows exactly how to make it robust in terms of lifecycle events like onPause and onResume. 然后,步骤3确切显示了如何在生命周期事件(如onPause和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. 逐步完成他们正确解决问题的过程,并适应您的情况并做同样的事情,您应该尽快将其收起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM