0

安卓 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)

关闭数据库和游标对象的最佳做法是什么?

4

1 回答 1

0

在 onPause() 中调用 close。当您更改移动设备的方向时,您的应用程序会更改状态,并且您应该在 android 系统调用 super.onPause() 之前关闭数据库/光标对象。在命令 super.onPause() 之前在 onPause 方法中调用 close。

于 2013-02-16T17:37:37.543 回答