活动生命周期和数据库
我有一个应用程序,它从数据库表(数据库名为 SoftCopyDatabase)读取值,并使用从数据库读取的值填充列表。单击列表中的某个项目后,新活动就会启动。
问题是当我按后退键时出现错误
IllegalStateException: database already closed
我的代码如下:
public class OpenClick extends ListActivity {
public static String subjectName;
private SoftCopyDatabase lectures;
private static int[] subTO = { R.id.subject };
private static String[] subFROM = { SUBJECT };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lectures = new SoftCopyDatabase(this);
}
public void onStart() {
super.onStart();
try {
Cursor cursor = getSubjects();
showSubjects(cursor);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onRestart() {
super.onRestart();
lectures = new SoftCopyDatabase(this);
}
public void onStop() {
super.onStop();
lectures.close();
}
public void onDestroy() {
super.onDestroy();
lectures.close();
}
//remaining code....
}
我想提的一点是,如果我删除 onStop() 方法,应用程序将正常工作。但我必须包括 onstop() 因为我想控制数据库的打开和关闭。
I have an application that reads values from a Database table (Database is named SoftCopyDatabase) and populates a List with the values read from Database. On clicking an item from the list a New Activity Starts.
The problem is when I press the back key I got an error
IllegalStateException: database already closed
My code is as follows:
public class OpenClick extends ListActivity {
public static String subjectName;
private SoftCopyDatabase lectures;
private static int[] subTO = { R.id.subject };
private static String[] subFROM = { SUBJECT };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lectures = new SoftCopyDatabase(this);
}
public void onStart() {
super.onStart();
try {
Cursor cursor = getSubjects();
showSubjects(cursor);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onRestart() {
super.onRestart();
lectures = new SoftCopyDatabase(this);
}
public void onStop() {
super.onStop();
lectures.close();
}
public void onDestroy() {
super.onDestroy();
lectures.close();
}
//remaining code....
}
One point I would like to mention is if I remove the onStop() Method the application is working properly. But I have to include onstop() because I want to control the opening and closing of Database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一篇涵盖该主题的好文章:
http ://awiden.wordpress.com/2010/03/26/database-mangement-and-the-activity-lifecycle/
Here is a good article covering the topic:
http://awiden.wordpress.com/2010/03/26/database-mangement-and-the-activity-lifecycle/
这是正常的,因为这两种方法都会执行...只需对这两种方法(
onDestroy
和onStop
)执行此操作:尽管我认为您可以只放置
.close< /code> 调用其中之一。确保在
SoftCopyDatabase
类中创建isOpen
方法,该方法必须调用SqliteDatabase
对象的isOpen
方法。That's normal because both methods are executed... just do this on both methods (
onDestroy
andonStop
):Although I think you can just put a
.close
invokation in one of them. Make sure to create theisOpen
method in yourSoftCopyDatabase
class which must call theSqliteDatabase
object'sisOpen
method.