Android 2.3.3SQLiteOpenHelper關閉()從未明確調用
我有兩個活動在我的應用程序訪問數據庫。無論何時我改變移動設備的方向,我都會在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)
什麼是關閉數據庫和光標對象的最佳實踐?
你會得到什麼錯誤? – 2013-02-16 17:21:14
用logcat跟蹤更新了問題。 – 2013-02-16 17:21:49
@VamsiChalla onPause()是更好的地方。 – 2013-02-16 17:36:28