如何在onrestart()中打开sqlite DB
我正在使用从数据库检索的数据更新主页列表视图。当我出去然后回到主页时它崩溃了
我的代码在这里
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart called");
DatabaseManager.getInstance(HomeActvity.this).open();
listAdapter.notifyDataSetChanged();
DatabaseManager.getInstance(HomeActvity.this).closeDatabase();
}
我的日志是:
11-28 19:07:22.606: E/AndroidRuntime(24303): java.lang.IllegalStateException: database not open
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1224)
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
我尝试了 runOnUIThread()..
I am updating home listview using data retrieved from DB. When I go forth then comeback to Home it crashes
My code is here
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart called");
DatabaseManager.getInstance(HomeActvity.this).open();
listAdapter.notifyDataSetChanged();
DatabaseManager.getInstance(HomeActvity.this).closeDatabase();
}
My Log is:
11-28 19:07:22.606: E/AndroidRuntime(24303): java.lang.IllegalStateException: database not open
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1224)
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
11-28 19:07:22.606: E/AndroidRuntime(24303): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
I tried runOnUIThread()..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么在notifydatachange后立即关闭数据库?我认为数据库读取是在关闭后发生的。尝试移除该线并查看其是否正常工作。
DatabaseManager.getInstance(HomeActvity.this).closeDatabase();Why do you close your database right after notifydatachanged? I think database reading is happening after close. Try to remove the line and see if it's working.
DatabaseManager.getInstance(HomeActvity.this).closeDatabase();在打开和关闭之间你什么也没做!怎么会..
You are doing nothing in between open and close! how come..