Android 2.3.3
我的应用程序中有两个访问数据库的活动。每当我改变移动设备的方向时,我在logcat中会遇到以下错误,尽管该程序确实有效。我从错误中了解到我必须调用close()
并关闭数据库和游标。但我在哪里准确地说出来。
关闭活动的onDestroy()
中的数据库和游标,是否解决了这个问题?
Database pm;
Cursor c;
@Override
public void onDestroy() {
pm.close();
c.close();
// Any other cursors that are used...
super.onDestroy();
}
这是需要的logcat跟踪...
02-16 22:37:50.419: E/Database(602): close() was never explicitly called on database 'portfolio'
02-16 22:37:50.419: E/Database(602): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:822)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:856)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:849)
02-16 22:37:50.419: E/Database(602): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:553)
02-16 22:37:50.419: E/Database(602): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-16 22:37:50.419: E/Database(602): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
02-16 22:37:50.419: E/Database(602): at com.xx.xxx.Database.<init>(Database.java:21)
02-16 22:37:50.419: E/Database(602): at com.xx.xxx.Brokerage_Settings.onCreate(Brokerage_Settings.java:118)
02-16 22:37:50.419: E/Database(602): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-16 22:37:50.419: E/Database(602): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 22:37:50.419: E/Database(602): at android.os.Looper.loop(Looper.java:130)
02-16 22:37:50.419: E/Database(602): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-16 22:37:50.419: E/Database(602): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 22:37:50.419: E/Database(602): at java.lang.reflect.Method.invoke(Method.java:507)
02-16 22:37:50.419: E/Database(602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-16 22:37:50.419: E/Database(602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-16 22:37:50.419: E/Database(602): at dalvik.system.NativeStart.main(Native Method)
关闭数据库和游标对象的最佳做法是什么?
答案 0 :(得分:0)
在onPause()中调用close。当您更改移动方向时,您的应用程序会更改状态,并且您应该在android系统调用super.onPause()之前关闭数据库/光标对象。在命令super.onPause()之前调用onPause方法中的close。