0

我在通过 handler.postdelayed(r, 30000) 运行在可运行对象中的异步任务中运行一些数据库内容。它会重复(如您所见,每 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)
4

2 回答 2

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

你确定你没有在使用前不小心关闭光标吗?那还是异步任务在某个时候启动了两次?

不管是什么原因,似乎代码在某些时候试图访问一个封闭的光标。

于 2013-09-24T15:52:28.710 回答
1

IllegalStateException由于数据库已经关闭,因此您会到达 logcat 的某个位置。确保在关闭数据库之前取消您的后台任务,这是在onDestroy您的活动中最近完成的。

于 2013-09-24T15:51:10.827 回答