活动生命周期和数据库

发布于 2024-12-01 00:16:55 字数 1137 浏览 1 评论 0原文

我有一个应用程序,它从数据库表(数据库名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2024-12-08 00:16:55

这是正常的,因为这两种方法都会执行...只需对这两种方法(onDestroyonStop)执行此操作:

if(lectures.isOpen()){
    lectures.close();
}

尽管我认为您可以只放置 .close< /code> 调用其中之一。确保在 SoftCopyDatabase 类中创建 isOpen 方法,该方法必须调用 SqliteDatabase 对象的 isOpen 方法。

That's normal because both methods are executed... just do this on both methods (onDestroy and onStop):

if(lectures.isOpen()){
    lectures.close();
}

Although I think you can just put a .close invokation in one of them. Make sure to create the isOpen method in your SoftCopyDatabase class which must call the SqliteDatabase object's isOpen method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文