我在Android上重新启用SQLite数据库时遇到问题。
我的例外:
15152/ch.companyname.projectname E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: ch.companyname.projectname, PID: 15117
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/ch.companyname.projectname/files/db_de.sqlite
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1349)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1292)
at com.j256.ormlite.android.compat.JellyBeanApiCompatibility.rawQuery(JellyBeanApiCompatibility.java:21)
at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:193)
at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:72)
at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:57)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:247)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:197)
at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:277)
at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:366)
at com.j256.ormlite.stmt.Where.query(Where.java:480)
at ch.companyname.projectname.business.database.DatabaseAccessBackend$5.queryInBackground(DatabaseAccessBackend.java:151)
at ch.companyname.projectname.business.database.DatabaseAccessBackend$5.queryInBackground(DatabaseAccessBackend.java:144)
at ch.companyname.projectname.business.database.QueryTask.doInBackground(QueryTask.java:28)
at ch.companyname.projectname.business.database.QueryTask.doInBackground(QueryTask.java:11)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
正如我发现的那样,当多个sqlite实例打开时会发生此异常。 Documentation
我的助手方法:
private DatabaseHelper getHelper() {
String locale = AppSettingsBackend.getInstance(PreferenceManager.getDefaultSharedPreferences(context)).getAppSettings().get().getLanguage();
if (this.databaseHelper == null || databaseLocale != locale) {
destroyCurrentHelper();
this.databaseLocale = locale;
DatabaseHelper.DATABASE_LOCALE = locale;
this.databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
}
return this.databaseHelper;
}
private void destroyCurrentHelper() {
if (this.databaseHelper != null) {
databaseHelper.close();
databaseHelper = null;
}
}
第151行的DatabaseAccessBackend中的代码:
public void getNavigationItemsForParentId(final int parentId, final QueryTask.Callback<List<NavigationItem>> callback, final List<Integer> ignoreIds) {
new QueryTask<List<NavigationItem>>() {
@Override
protected List<NavigationItem> queryInBackground() throws SQLException {
Where<NavigationItem, Integer> query = getHelper().getNavigationItemDao().queryBuilder().where().eq("ZPARENTID", parentId);
if (ignoreIds != null && ignoreIds.size() > 0) {
query.and().notIn("ZID", ignoreIds);
}
return query.query();
}
}.start(callback);
}
答案 0 :(得分:1)
我发现了错误:
只需更改
databaseHelper.close()
到
OpenHelperManager.releaseHelper();