我经常遇到以下异常,我完全确定该怎么做。
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: android.database.sqlite.SQLiteException: error code 5: database is locked
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1704)
at azurewing.android.db.provider.NotificationProvider.delete(NotificationProvider.java:80)
at azurewing.android.db.provider.Provider.delete(Provider.java:87)
at azurewing.android.db.provider.NotificationProvider.delete(NotificationProvider.java:1)
at android.content.ContentProvider$Transport.delete(ContentProvider.java:234)
at android.content.ContentResolver.delete(ContentResolver.java:692)
at azurewing.android.db.table.NotificationTable.removeAllNotifications(NotificationTable.java:89)
at azurewing.android.sync.SyncReceiver$1$1.doInBackground(SyncReceiver.java:52)
at azurewing.android.sync.SyncReceiver$1$1.doInBackground(SyncReceiver.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more
我正在通过 AsyncTask 访问 ContentProvider(此处为 NotificationProvider)。ContentProvider 当然使用数据库。
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
SQLiteDatabase db = database.getWritableDatabase();
int numDeleted = database.delete(NotificationTable.TABLE_NAME, selection, selectionArgs);
return numDeleted;
}
我有点困惑在这里做什么。我认为这显然是一个线程问题,所以我在每次获取数据库时都在考虑,我是从 1 个类中的同步方法中完成的。这是一个好主意吗?