java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象
|
我试图弄清楚为什么有时会收到IllegalStateException。我找不到任何好的示例来说明如何使用线程来查询SQLite数据库来加载列表。我在下面包含了我的代码。在大多数情况下,它可以正常运行,但是偶尔,我会收到IllegalStateException。
我的另一个活动(也是ExpandableListActivity的一个实例)也得到了类似的例外。该异常指出“试图重新查询已经关闭的游标”。
有人可以告诉我正确的方法来做到这一点,以免引起任何错误吗?我宁愿使用游标,而不是将所有数据复制到内存中。如果无法弄清楚,则必须将其全部加载到内存中。
我认为此问题与startManagingCursor(Cursor)和onDestroy()中的数据库连接已关闭有关。
请帮助
-故事
public class MyListActivity extends ListActivity {
private MyCursorAdapter adapter;
private SQLiteDatabase db = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
new RetrieveCursorTask(this).execute((Void[]) null);
} catch (Exception e) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Null out the cursor.
if (adapter != null) {
adapter.changeCursor(null);
adapter = null;
}
if (db != null && db.isOpen()) {
db.close();
}
}
private class RetrieveCursorTask extends AsyncTask<Void, Void, Cursor> {
private Context ctx;
public RetrieveCursorTask(Context ctx) {
this.ctx = ctx;
}
@Override
protected Cursor doInBackground(Void... params) {
Cursor cursor = null;
DbHelper helper = new DbHelper(ctx);
try {
db = helper.getReadableDatabase();
cursor = db.query(\"users\",
new String[] {
DbHelper.ID_COLUMN,
DbHelper.UID_COLUMN
},
null, null, null, null, null);
startManagingCursor(cursor);
} catch (Exception e) {
}
return cursor;
}
@Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
if (cursor != null) {
try {
adapter = new MyCursorAdapter(ctx, cursor);
} catch (Exception e) {
}
setListAdapter(adapter);
}
}
}
private class MyCursorAdapter extends CursorAdapter {
private Context ctx;
public MyCursorAdapter(Context context, Cursor c) {
super(context, c);
this.ctx = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// ...
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// ...
}
}
}
没有找到相关结果
已邀请:
2 个回复
琳娘
棠媳鳖