最近在改應用的一些bug,想起來可以把常見錯誤整理下,方便自己下次更快速找出錯誤原因,也給遇到同樣問題的同學提供解決思路。我會將錯誤歸類,今天先寫一點,以後再增加,歡迎指正。(為了不透漏一些資訊,log中包名被修改了)
1. android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
[java]
view plaincopy
- FATAL EXCEPTION: main
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evaluation/com.evaluation.VehicleActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
- at android.app.ActivityThread.access$2300(ActivityThread.java:135)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
- at android.os.Handler.dispatchMessage(Handler.java:99)
- at android.os.Looper.loop(Looper.java:143)
- at android.app.ActivityThread.main(ActivityThread.java:4914)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:521)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
- 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:363)
- at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)
- at android.database.sqlite.SQLiteProgram.onAllReferencesReleased(SQLiteProgram.java:116)
- at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)
- at android.database.sqlite.SQLiteProgram.close(SQLiteProgram.java:293)
- at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:133)
- at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:532)
- at com.DatabaseHelper.selectAssessInformation(DatabaseHelper.java:338)
- at com.JSKApplication.selectAssessInformation(JSKApplication.java:631)
- at com.VehicleActivity.onCreate(VehicleActivity.java:548)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
- ... 11 more
這個錯誤翻譯過來就是:不能關閉資料庫由於未完成的語句。
我從網上查了一下,最可能的原因是:你在操作sqlite資料庫時使用多線程了,但sqlite資料庫是不支援多線程操作,所以你必須實現多線程同步的機制。
我的程式出錯是因為:在後台啟動了個service定時向伺服器請求資料,並線上程中將資料插入資料庫。在某個時間段,使用者在向資料庫插入資料後,關閉資料庫,而此時線程正在向資料庫中寫入資料,就會造成上述異常。
2.android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
[java]
view plaincopy
- 07-04 16:18:56.677: W/System.err(29830): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
約束失敗,導致這類錯誤,原因一般有兩個:
1)插入的資料有一個是主鍵,而且插入的主鍵相同;
2)插入的資料有一條資料為空白,而資料庫中定義不可為空,也會導致這樣的錯誤;
最近在改應用的一些bug,想起來可以把常見錯誤整理下,方便自己下次更快速找出錯誤原因,也給遇到同樣問題的同學提供解決思路。我會將錯誤歸類,今天先寫一點,以後再增加,歡迎指正。(為了不透漏一些資訊,log中包名被修改了)
1. android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
[java]
view plaincopy
- FATAL EXCEPTION: main
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evaluation/com.evaluation.VehicleActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
- at android.app.ActivityThread.access$2300(ActivityThread.java:135)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
- at android.os.Handler.dispatchMessage(Handler.java:99)
- at android.os.Looper.loop(Looper.java:143)
- at android.app.ActivityThread.main(ActivityThread.java:4914)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:521)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
- 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:363)
- at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)
- at android.database.sqlite.SQLiteProgram.onAllReferencesReleased(SQLiteProgram.java:116)
- at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:45)
- at android.database.sqlite.SQLiteProgram.close(SQLiteProgram.java:293)
- at android.database.sqlite.SQLiteQuery.close(SQLiteQuery.java:133)
- at android.database.sqlite.SQLiteCursor.close(SQLiteCursor.java:532)
- at com.DatabaseHelper.selectAssessInformation(DatabaseHelper.java:338)
- at com.JSKApplication.selectAssessInformation(JSKApplication.java:631)
- at com.VehicleActivity.onCreate(VehicleActivity.java:548)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
- ... 11 more
這個錯誤翻譯過來就是:不能關閉資料庫由於未完成的語句。
我從網上查了一下,最可能的原因是:你在操作sqlite資料庫時使用多線程了,但sqlite資料庫是不支援多線程操作,所以你必須實現多線程同步的機制。
我的程式出錯是因為:在後台啟動了個service定時向伺服器請求資料,並線上程中將資料插入資料庫。在某個時間段,使用者在向資料庫插入資料後,關閉資料庫,而此時線程正在向資料庫中寫入資料,就會造成上述異常。
2.android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
[java]
view plaincopy
- 07-04 16:18:56.677: W/System.err(29830): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
約束失敗,導致這類錯誤,原因一般有兩個:
1)插入的資料有一個是主鍵,而且插入的主鍵相同;
2)插入的資料有一條資料為空白,而資料庫中定義不可為空,也會導致這樣的錯誤;