gpt4 book ai didi

android IllegalStateException,数据库已经关闭

转载 作者:行者123 更新时间:2023-11-29 16:21:24 25 4
gpt4 key购买 nike

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

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

}
}


}

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

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

最佳答案

finally
{
lectures.close();
}

我认为这一行给出了异常,所以把它放在 onDestroy() 中。

关于android IllegalStateException,数据库已经关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com