为什么有些用户在database.close上获得FC?

时间:2011-06-23 18:24:43

标签: android database

我有以下代码:

public class DbAdapter {
...
    public DbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }
...

这个close方法在onAtroy of MainActivity中调用:

@Override
protected void onDestroy() {
  super.onDestroy();

  mDbHelper.close();
}

我的用户输入了以下错误日志:

device_model:bravo
build_version:1.6.17
condition:1
processName:spb.bridges
pid:23493
uid:10057
tag:null
shortMsg:android.database.sqlite.SQLiteException
longMsg:android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
stackTrace:java.lang.RuntimeException: Unable to destroy activity {my.package/my.package.MainActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2680)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2698)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3694)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)
at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:323)
at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:884)
at android.database.sqlite.SQLiteOpenHelper.close(SQLiteOpenHelper.java:220)
at spb.bridges.DbAdapter.close(DbAdapter.java:177)
at spb.bridges.MainActivity.onDestroy(MainActivity.java:159)

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

当应用程序执行数据库操作时,他们可能会尝试关闭应用程序。最简单的方法是在mDbHelper.close();语句周围放置一个try / catch / log。

答案 1 :(得分:0)

不保证运行OnDestroy()。如果说用户正在使用任务杀手,它可能会杀死-9,而不是运行。 SO上的其他人也表示有些情况甚至Android都不会打电话给它。它应该在android杀死它以释放内存之前被调用。

最好是在OnPause()中关闭该逻辑然后再在OnResume()中初始化。