2011-06-23 41 views
0

我有以下代碼:爲什麼有些用戶在database.close上獲得FC?

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

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

此調用close方法在MainActivity中的onDestroy:

@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) 

我的代碼有什麼問題?

回答

1

他們可能會在應用程序執行數據庫操作時嘗試關閉應用程序。要做的最簡單的事情就是圍繞mDbHelper.close();聲明放置try/catch/log。

+0

應該捕捉什麼樣的異常? –

+0

'SQLiteException'或'Exception'本身。這是不好的做法,但比扔掉「關閉」力量要好。其根本原因可能是它試圖在'.close()'時執行數據庫操作。你有長時間運行的DB操作嗎? – Haphazard

0

OnDestroy()不保證運行。如果說一個用戶正在使用一個任務殺手,它可能會殺死這個東西,而不是運行。 SO上的其他人也表示,即使Android不會調用它,情況也是如此。它應該在android殺死它釋放內存之前被調用。

如果你把這個邏輯關閉到OnPause()然後在OnResume()中再次初始化,它可能是最好的。

相關問題