SQLiteDBを正しく閉じる方法に問題があります。
アプリケーションを実行すると、アクティビティの1つがDBAdapter
クラスを使用してDBから情報の行を取得し、ユーザーが戻るボタンを押したりアプリを閉じたりした場合の問題を回避するために、すぐに閉じたいと考えています。
しかし、同じDBとアダプターを使用する別のアクティビティに切り替えると、エラーが発生します。
アクティビティレベルのコードは次のとおりです。
PlayerSummonDbAdapter PlayerDbAdapter;
PlayerDbAdapter = new PlayerSummonDbAdapter(this);
PlayerDbAdapter.open();
mySummon = new Summon();
mySummon = PlayerDbAdapter.retrieveSummon(1, summonName);
PlayerDbAdapter.close();
そして、これが私のアダプターのメソッドopen()
とメソッドです。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)