我无法正确关闭我的 SQLite DB。
当我运行我的应用程序时,其中一个活动使用我的DBAdapter
类从我的数据库中检索一行信息,然后我想立即关闭它以避免在用户点击后退按钮或关闭应用程序时出现任何问题。
但是当我切换到另一个使用相同数据库和适配器的活动时,我得到一个错误。
这是活动级别代码:
PlayerSummonDbAdapter PlayerDbAdapter;
PlayerDbAdapter = new PlayerSummonDbAdapter(this);
PlayerDbAdapter.open();
mySummon = new Summon();
mySummon = PlayerDbAdapter.retrieveSummon(1, summonName);
PlayerDbAdapter.close();
这是我的适配器中的open()
and方法:lose()
public PlayerSummonDbAdapter open() throws SQLException {
dbHelper = new PlayerSummonSQLiteHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
database.close();
dbHelper.close();
}
这是错误日志的一个示例:它说close() 没有在数据库上调用,但我确实关闭了它。至少我认为我做到了。
06-21 00:10:35.928: I/dalvikvm(23203): Uncaught exception thrown by finalizer (will be discarded):
06-21 00:10:35.928: I/dalvikvm(23203): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@40567168 on Moves that has not been deactivated or closed
06-21 00:10:35.928: I/dalvikvm(23203): at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
06-21 00:10:35.928: I/dalvikvm(23203): at dalvik.system.NativeStart.run(Native Method)
06-21 00:10:35.932: E/Database(23203): close() was never explicitly called on database '/mnt/sdcard/Summon.sqlite'
06-21 00:10:35.932: E/Database(23203): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-21 00:10:35.932: E/Database(23203): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
06-21 00:10:35.932: E/Database(23203): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
06-21 00:10:35.932: E/Database(23203): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
06-21 00:10:35.932: E/Database(23203): at com.willmer.SummonListAdapter.open(SummonListAdapter.java:55)
06-21 00:10:35.932: E/Database(23203): at com.willmer.BattleActivity.onCreate(BattleActivity.java:78)
06-21 00:10:35.932: E/Database(23203): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-21 00:10:35.932: E/Database(23203): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-21 00:10:35.932: E/Database(23203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-21 00:10:35.932: E/Database(23203): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-21 00:10:35.932: E/Database(23203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-21 00:10:35.932: E/Database(23203): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 00:10:35.932: E/Database(23203): at android.os.Looper.loop(Looper.java:130)
06-21 00:10:35.932: E/Database(23203): at android.app.ActivityThread.main(ActivityThread.java:3688)
06-21 00:10:35.932: E/Database(23203): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 00:10:35.932: E/Database(23203): at java.lang.reflect.Method.invoke(Method.java:507)
06-21 00:10:35.932: E/Database(23203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-21 00:10:35.932: E/Database(23203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-21 00:10:35.932: E/Database(23203): at dalvik.system.NativeStart.main(Native Method)
06-21 00:10:35.944: E/Database(23203): close() was never explicitly called on database '/data/data/com.willmer/databases/PlayerSummons.db'
06-21 00:10:35.944: E/Database(23203): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-21 00:10:35.944: E/Database(23203): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
06-21 00:10:35.944: E/Database(23203): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
06-21 00:10:35.944: E/Database(23203): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
06-21 00:10:35.944: E/Database(23203): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
06-21 00:10:35.944: E/Database(23203): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:635)
06-21 00:10:35.944: E/Database(23203): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-21 00:10:35.944: E/Database(23203): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
06-21 00:10:35.944: E/Database(23203): at com.willmer.PlayerSummonDbAdapter.open(PlayerSummonDbAdapter.java:67)
06-21 00:10:35.944: E/Database(23203): at com.willmer.BattleActivity.CaptureSummon(BattleActivity.java:563)
06-21 00:10:35.944: E/Database(23203): at com.willmer.BattleActivity.onClick(BattleActivity.java:146)
06-21 00:10:35.944: E/Database(23203): at android.view.View.performClick(View.java:2538)
06-21 00:10:35.944: E/Database(23203): at android.view.View$PerformClick.run(View.java:9152)
06-21 00:10:35.944: E/Database(23203): at android.os.Handler.handleCallback(Handler.java:587)
06-21 00:10:35.944: E/Database(23203): at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 00:10:35.944: E/Database(23203): at android.os.Looper.loop(Looper.java:130)
06-21 00:10:35.944: E/Database(23203): at android.app.ActivityThread.main(ActivityThread.java:3688)
06-21 00:10:35.944: E/Database(23203): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 00:10:35.944: E/Database(23203): at java.lang.reflect.Method.invoke(Method.java:507)
06-21 00:10:35.944: E/Database(23203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-21 00:10:35.944: E/Database(23203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-21 00:10:35.944: E/Database(23203): at dalvik.system.NativeStart.main(Native Method)