2013-02-16 64 views
0

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) 

什麼是關閉數據庫和光標對象的最佳實踐?

+0

你會得到什麼錯誤? – 2013-02-16 17:21:14

+0

用logcat跟蹤更新了問題。 – 2013-02-16 17:21:49

+1

@VamsiChalla onPause()是更好的地方。 – 2013-02-16 17:36:28

回答

0

在onPause()中調用close。當您更改移動的方向時,您的應用會更改狀態,並且您應該在android系統調用super.onPause()之前關閉數據庫/光標對象。在命令super.onPause()之前用onPause方法調用close。

+0

它工作!謝謝.. – 2013-02-16 17:41:07