android IllegalStateException,数据库已关闭

发布于 2024-11-30 10:18:22 字数 2094 浏览 2 评论 0原文

我正在使用 SimpleCursorAdapter 和数据库表来填充列表。列表被填充,我可以单击列表项来打开所需的项目(这将启动一个新活动)。问题是当我按返回键时,出现以下错误。

IllegalStateException:数据库已关闭。

我的代码如下:

public class populatingLectures extends ListActivity{

private static String[] FROM = {SUBJECT, TOPIC, LECTURENUMBER, DATE };
private static int[] TO = {R.id.subject, R.id.topic,
        R.id.lecturenumber, R.id.date };
private static String[] data = { SUBJECT, TOPIC, LECTURENUMBER, _DATA };
private static String ORDER_BY = DATE + " DESC";
private SoftCopyDatabase lectures;  
String gotId;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SoftCopyDatabase lectures = new SoftCopyDatabase(this);
    try {
        Cursor cursor = getLectures();
        showLectures(cursor);
    } finally {
        lectures.close();
    }
}

public void onStart() {
    super.onStart();
    lectures = new SoftCopyDatabase(this);
    try {
        Cursor cursor = getLectures();
        showLectures(cursor);
    } finally {
        lectures.close();
    }
}

private Cursor getLectures() {

    SQLiteDatabase db = lectures.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME,null, "subject=?", new String[] {OpenClick.subjectName}, null, null,
            ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
}


private void showLectures(Cursor cursor) {

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.item_lectures, cursor, FROM, TO);
    setListAdapter(adapter);
}

private Cursor getFileName(String ID) {

    SQLiteDatabase db = lectures.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, data, "_ID=?",
            new String[] { ID }, null, null, null);
    startManagingCursor(cursor);
    return cursor;
}


@Override
protected void onListItemClick(ListView listView, View view, int position,
        long id) {
    super.onListItemClick(listView, view, position, id);

             //...CODE TO START NEW ACTIVITY

    }
}


}

请告诉我我犯了什么错误。因为我没有在任何地方显式关闭数据库。

问候, 瓦内亚·伊克巴尔。

I am using SimpleCursorAdapter and database table to populate a List. The list gets populated and I am able to click on list items to open the desired item(This starts a new activity). The problem is when I press the back key, I got Following error.

IllegalStateException: database already closed.

My code is as follows:

public class populatingLectures extends ListActivity{

private static String[] FROM = {SUBJECT, TOPIC, LECTURENUMBER, DATE };
private static int[] TO = {R.id.subject, R.id.topic,
        R.id.lecturenumber, R.id.date };
private static String[] data = { SUBJECT, TOPIC, LECTURENUMBER, _DATA };
private static String ORDER_BY = DATE + " DESC";
private SoftCopyDatabase lectures;  
String gotId;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SoftCopyDatabase lectures = new SoftCopyDatabase(this);
    try {
        Cursor cursor = getLectures();
        showLectures(cursor);
    } finally {
        lectures.close();
    }
}

public void onStart() {
    super.onStart();
    lectures = new SoftCopyDatabase(this);
    try {
        Cursor cursor = getLectures();
        showLectures(cursor);
    } finally {
        lectures.close();
    }
}

private Cursor getLectures() {

    SQLiteDatabase db = lectures.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NAME,null, "subject=?", new String[] {OpenClick.subjectName}, null, null,
            ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
}


private void showLectures(Cursor cursor) {

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.item_lectures, cursor, FROM, TO);
    setListAdapter(adapter);
}

private Cursor getFileName(String ID) {

    SQLiteDatabase db = lectures.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, data, "_ID=?",
            new String[] { ID }, null, null, null);
    startManagingCursor(cursor);
    return cursor;
}


@Override
protected void onListItemClick(ListView listView, View view, int position,
        long id) {
    super.onListItemClick(listView, view, position, id);

             //...CODE TO START NEW ACTIVITY

    }
}


}

Kindly tell me what mistake am I doing. Because I am not closing the database explicitly any where.

Regards,
Waneya Iqbal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦魇绽荼蘼 2024-12-07 10:18:22
finally
{       
 lectures.close();
}

我认为这一行给出了异常,因此将其放入 onDestroy() 中。

finally
{       
 lectures.close();
}

I think this line gives the exception, so put it in onDestroy().

番薯 2024-12-07 10:18:22

填充列表后,您将立即关闭数据库。您应该将该行

lectures.close() 

从 onStart() 移至 onDestroy(),这将确保您的数据库在活动完成时关闭,而不是在列表填充后立即关闭。

You're closing the database as soon as you populate the list. You should move the

lectures.close() 

line from onStart() to onDestroy(), which will make sure your database gets closed when the activity is complete, rather than as soon as the list is populated.

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