更新 SQLite 数据库时出现问题

发布于 2024-10-20 03:15:50 字数 3056 浏览 1 评论 0原文

我有一个从我的个人 SQLite 数据库填充的列表视图,显示有关事件的信息。我正在尝试使用事件描述页面上的复选框创建一种“收藏”此事件的方法。当用户选中复选框然后启动 onPause() 时,代码将尝试更新数据库

我已经查找了一些更新数据库中行的示例,但无论我尝试什么,我似乎总是遇到 NullPointerException 问题。

如果您需要查看任何其他代码来帮助我解决此问题,请告诉我。

这是当用户离开描述活动以查看他们是否选中了复选框时我调用的代码:

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

这是 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);
    }
}

这是来自 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)

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.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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-10-27 03:15:50

因此,问题的真正根本原因似乎很简单:您创建了一个新的 dbHelper,但从未显式打开数据库。您看起来与标准 Android 记事本教程非常相似。将其拉出来并查看 NotesDbAdaptor 如何包装 SQLiteOpenHelper。您需要先调用 myDbHelper.open(),然后才能使用 myDbHelper。

您应该进行的另一个关键更改是遵循记事本示例中所示的相同模式。您应该将 db 保留为成员变量 - 使用 new 初始化,然后在 onCreate() 期间打开它。 (打开数据库可能需要半秒钟。在初始化时执行一次比每次更改时执行一次更好。)请参阅 Notepadv1.java 中的示例。

同样,这个记事本示例看起来与您正在做的事情非常相似。在教程的第 2 步中,他们会打开一个详细信息页面来编辑注释,就像您在...上打开一个详细信息页面一样。然后,步骤 3 准确展示了如何使其在 onPause 和 onResume 等生命周期事件方面变得稳健。第一次做的时候会很棘手。逐步完成他们的正确过程,并适应您的情况并做同样的事情,您应该很快就会学会。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文