我无法正常关闭SQLite数据库。
当我运行我的应用程序时,其中一个活动使用我的DBAdapter
类从我的数据库中检索一行信息然后我想立即关闭它以避免任何问题如果用户击中后面按钮或关闭应用程序。
但是当我切换到使用相同数据库和适配器的另一个活动时,我收到错误。
以下是活动级别代码:
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)
答案 0 :(得分:0)
好像你没有关闭使用的光标。
注意:android支持库中有更好的API可以解决大部分问题。
答案 1 :(得分:0)
尝试在onDestroy
中关闭数据库。
将此添加到您的代码中
@Override
protected void onDestroy() {
super.onDestroy();
if (dbHelper != null) {
database.close();
dbHelper .close();
}
}
答案 2 :(得分:0)
这出现了很多,但基本上是:
finally
区块内)打开数据库有一些开销,因此反复打开和关闭不是一个好主意。使您的数据库帮助程序成为单例,并在整个应用程序中使用它。不要在您的应用中调用其close()
方法。