错误仅在我深入4个活动并尝试退出时发生。我尝试了各种方法来打开和关闭数据库,包括将close()从onDestroy()移动到onPause()方法,并在onResume()中添加另一个open()。
此外,我的活动大量使用ListViews和ExpandableListViews,根据这篇文章,我理解这可能会导致数据库关闭: http://darutk-oboegaki.blogspot.com/2011/03/sqlitedatabase-is-closed-automatically.html 我已经检查了代码,确保我要么关闭了所有游标,要么如果它们被分配给适配器,则调用startManagingCursor()。
有人知道发生了什么吗?
java.lang.RuntimeException: Unable to resume activity {com.fieldone/com.fieldone.DispatchActivity}: java.lang.IllegalStateException: database /data/data/com.fieldone/databases/InterstateAirConditioning-1602814322.db already closed
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3347)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3362)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2162)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: database /data/data/com.fieldone/databases/InterstateAirConditioning-1602814322.db already closed
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:145)
at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:567)
at android.app.Activity.performRestart(Activity.java:3836)
at android.app.Activity.performResume(Activity.java:3857)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3337)
... 10 more
更新:我已经解决了问题,但不确定为什么会这样。也许有人知道或能够解释一下。
当我在活动堆栈的第四个活动中时,我试图通过db.close()关闭数据库。无论我把它放在哪里,在获取所需数据后的onCreate中,还是在onStop或onDestroy中,都会产生此错误。如果我不关闭数据库,我就不会遇到这个问题。因此,某些东西会导致数据库自动关闭。奇怪的是,虽然我在最终活动中使用了可扩展列表视图,但我没有使用游标适配器。有人有什么想法吗?希望能理解这个问题。