java.lang.IllegalStateException:尝试重新查询已关闭的游标android.database



我已经阅读了几篇相关帖子,甚至在这里发布并回答了这个问题,但似乎我无法解决问题。

我有3项活动:第一幕(主要)第二幕第三幕

来回使用 Act1->Act2

和 Act2->Act1 时,我没有任何问题当去 Act2->Act3 时,我没有遇到任何问题当使用Act3->Act2时,我偶尔会崩溃并显示以下错误:java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@...。这是一个列表视图光标。

我尝试过: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);
}
  1. 从Act3返回Act2时,我会执行以下操作:

    private void populateCompetitorsListView(){ ListAdapter currentListAdapter = getListAdapter(); 光标当前光标 = 空; 游标锦标赛股票游标 = 空;

    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中,我使用以下意图,但预期没有结果:

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);
}

移动法案1->Act2 Act2->Act1从不给我带来麻烦。我在那里使用startActivityForResult(intent, ACTIVITY_EDIT);,但我不确定 - 这可能是我麻烦的根源吗?

如果有人能就这个问题有所了解,我将不胜感激。我有兴趣了解更多关于这个主题的信息。

谢谢,D。

我称之为二维问题:有两件事导致了这次崩溃: 1.我在不应该使用的地方使用了startManagingCursor(mItemCursor);。 2. 我忘了在onResume()上初始化光标适配器()(用于自动完成)

//@SuppressWarnings("deprecation")
private void initCursorAdapter()
{
    mItemCursor = mDbHelper.getCompetitorsCursor("");      
    startManagingCursor(mItemCursor); //<= this is bad!
    mCursorAdapter = new CompetitorAdapter(getApplicationContext(), mItemCursor);       
    initItemFilter();
}

现在它似乎工作正常。希望如此。。。

这样说吧,它可能对你有用:

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
            orderCursor.requery();
    }

这也有效

       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            startManagingCursor(Cursor);
        }

最新更新