我已阅读了几篇相关帖子,甚至发布并回复了here,但似乎我无法解决问题。
我有3项活动: Act1(主要) ACT2 ACT3
当来回行动Act1-> Act2和Act2-> Act1时,我没有遇到任何问题
当进行Act2-> Act3时,我没有遇到任何问题
当进行Act3-> Act2时,我偶尔会遇到以下错误:java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@...
。这是一个ListView游标。
我尝试了什么:
1.将stopManagingCursor(currentCursor);
添加到Act2的onPause()中,以便在将Act2保留为Act3时停止管理光标
protected void onPause()
{
Log.i(getClass().getName() + ".onPause", "Hi!");
super.onPause();
saveState();
//Make sure you get rid of the cursor when leaving to another Activity
//Prevents: ...Unable to resume activity... trying to requery an already closed cursor
Cursor currentCursor = ((SimpleCursorAdapter)getListAdapter()).getCursor();
stopManagingCursor(currentCursor);
}
从Act3返回到Act2时,我会执行以下操作:
private void populateCompetitorsListView() { ListAdapter currentListAdapter = getListAdapter(); 游标currentCursor = null; 游标tournamentStocksCursor = null;
if(currentListAdapter != null)
{
currentCursor = ((SimpleCursorAdapter)currentListAdapter).getCursor();
if(currentCursor != null)
{
//might be redundant, not sure
stopManagingCursor(currentCursor);
// Get all of the stocks from the database and create the item list
tournamentStocksCursor = mDbHelper.retrieveTrounamentStocks(mTournamentRowId);
((SimpleCursorAdapter)currentListAdapter).changeCursor(tournamentStocksCursor);
}
else
{
tournamentStocksCursor = mDbHelper.retrieveTrounamentStocks(mTournamentRowId);
}
}
else
{
tournamentStocksCursor = mDbHelper.retrieveTrounamentStocks(mTournamentRowId);
}
startManagingCursor(tournamentStocksCursor);
//Create an array to specify the fields we want to display in the list (only name)
String[] from = new String[] {StournamentConstants.TblStocks.COLUMN_NAME, StournamentConstants.TblTournamentsStocks.COLUMN_SCORE};
// and an array of the fields we want to bind those fields to (in this case just name)
int[] to = new int[]{R.id.competitor_name, R.id.competitor_score};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter tournamentStocks = new SimpleCursorAdapter(this, R.layout.competitor_row, tournamentStocksCursor, from, to);
//tournamentStocks.convertToString(tournamentStocksCursor);
setListAdapter(tournamentStocks);
}
所以我确保使光标无效并使用另一个光标。我发现当我进入Act3-> Act2时,系统有时会对列表视图使用相同的光标,有时会有不同的光标。
这很难调试,我在调试时始终无法捕获崩溃系统。我怀疑这与调试所花费的时间(长)以及运行应用程序所需的时间(更短,没有因断点而暂停)有关。
在Act2中,我使用以下Intent并期望没有结果:
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this, ActivityCompetitorDetails.class);
intent.putExtra(StournamentConstants.App.competitorId, id);
intent.putExtra(StournamentConstants.App.tournamentId, mTournamentRowId);
startActivity(intent);
}
移动Act1-> Act2 Act2-> Act1永远不会给我带来麻烦。在那里,我使用startActivityForResult(intent, ACTIVITY_EDIT);
并且我不确定 - 这可能是我麻烦的根源吗?
如果有人能对这个问题有所了解,我将不胜感激。我有兴趣更多地了解这个主题。
谢谢,d。
答案 0 :(得分:1)
我将此称为二维问题:造成此次崩溃的原因有两件事:
我用了startManagingCursor(mItemCursor);
我不应该的地方。
2.我忘了在onResume()
//@SuppressWarnings("deprecation")
private void initCursorAdapter()
{
mItemCursor = mDbHelper.getCompetitorsCursor("");
startManagingCursor(mItemCursor); //<= this is bad!
mCursorAdapter = new CompetitorAdapter(getApplicationContext(), mItemCursor);
initItemFilter();
}
现在似乎工作正常。我希望如此......
答案 1 :(得分:1)
这个可能对你有用:
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
orderCursor.requery();
}
答案 2 :(得分:0)
这也有效
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
startManagingCursor(Cursor);
}