asynctask中的SqLite数据库错误

时间:2013-09-24 15:38:54

标签: android sqlite android-asynctask

我在asnctask中运行一些数据库内容,它通过handler.postdelayed(r,30000)在runnable中运行。它会重复(你可以看到,每30秒)。

有时,不是每次,我的应用都崩溃了。下面是logcat。

09-24 18:28:52.813: W/dalvikvm(6194): threadid=12: thread exiting with uncaught exception (group=0x40daf1f8)
09-24 18:28:52.823: E/AndroidRuntime(6194): FATAL EXCEPTION: AsyncTask #2
09-24 18:28:52.823: E/AndroidRuntime(6194): java.lang.RuntimeException: An error occured while executing doInBackground()
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.lang.Thread.run(Thread.java:856)
09-24 18:28:52.823: E/AndroidRuntime(6194): Caused by: java.lang.IllegalStateException: database /data/data/com.myApp/databases/mydb.db (conn# 0) already closed
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2100)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:418)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:405)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at com.myApp.DbAllHelper.dbMethod1(DbAllHelper.java:273)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at com.myApp.DbAllHelper.dbMethod2(DbAllHelper.java:582)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at com.myApp.MainActivity$checkToInternetOnly.doInBackground(MainActivity.java:278)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at com.myApp.MainActivity$checkToInternetOnly.doInBackground(MainActivity.java:1)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-24 18:28:52.823: E/AndroidRuntime(6194):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

2 个答案:

答案 0 :(得分:1)

因为数据库已经关闭,所以你可以在logcat上找到一个IllegalStateException。在关闭数据库之前,请确保取消后台任务,这是在您的活动的onDestroy中最新完成的。

答案 1 :(得分:1)

09-24 18:28:52.823: E/AndroidRuntime(6194): Caused by: java.lang.IllegalStateException: database /data/data/com.myApp/databases/mydb.db (conn# 0) already closed

您确定在使用前没有意外关闭光标。这或任何机会的asynctask在某个时刻开始两次?

无论原因是什么,似乎代码在某个时候试图访问一个封闭的Cursor。