我收到此错误
10-11 13:58:46.713: E/AndroidRuntime(29407): FATAL EXCEPTION: AsyncTask #2
10-11 13:58:46.713: E/AndroidRuntime(29407): java.lang.RuntimeException: An error occured while executing doInBackground()
10-11 13:58:46.713: E/AndroidRuntime(29407): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-11 13:58:46.713: E/AndroidRuntime(29407): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.lang.Thread.run(Thread.java:856)
10-11 13:58:46.713: E/AndroidRuntime(29407): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.philly.philly/databases/PhillyORM.db
10-11 13:58:46.713: E/AndroidRuntime(29407): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
10-11 13:58:46.713: E/AndroidRuntime(29407): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1310)
10-11 13:58:46.713: E/AndroidRuntime(29407): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:154)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:206)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:155)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:261)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:262)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.stmt.Where.query(Where.java:482)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.j256.ormlite.dao.BaseDaoImpl.queryForEq(BaseDaoImpl.java:241)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.philly.philly.Repository.saveOrUpdateKontext(Repository.java:180)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.philly.philly.PostListActivity$LoadPostsTask.doInBackground(PostListActivity.java:161)
10-11 13:58:46.713: E/AndroidRuntime(29407): at com.philly.philly.PostListActivity$LoadPostsTask.doInBackground(PostListActivity.java:1)
10-11 13:58:46.713: E/AndroidRuntime(29407): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-11 13:58:46.713: E/AndroidRuntime(29407): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-11 13:58:46.713: E/AndroidRuntime(29407): ... 5 more
我认为关键部分是10-11 13:58:46.713: E/AndroidRuntime(29407): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.philly.philly/databases/PhillyORM.db
当异步任务从远程服务器检索数据时,我旋转屏幕时会发生这种情况。
我假设发生的事情是请求回来并尝试写入数据库,但启动任务的活动已被销毁并重新启动。
如果您需要查看我的代码,我会添加它,但我认为这个问题的来源实际上可能非常清楚,只是从错误消息和我的描述。
我正在寻找在检索远程数据和使用ORMLite写入本地数据库时如何正确处理屏幕旋转。也许我应该使用服务而不是异步任务?
提前致谢。
答案 0 :(得分:1)
您的活动可能正在关闭轮换数据库。 我的建议是使用AsyncTaskLoader,它将自己处理旋转。 http://developer.android.com/guide/components/loaders.html
我从经验中学到的一个提示是拥有一个单例数据库对象,只能在应用程序的整个生命周期中打开一次......当应用程序死掉时,对象会自动关闭。
答案 1 :(得分:0)
最好使用服务来处理数据库。当您旋转屏幕当前布局时,所有组件都将被销毁,并且使用新的方向布局(将具有其他组件)构建新组件。如果你没有真正使用它,你也可以选择阻止屏幕旋转。