2012-06-06 70 views
2

我已經閱讀了幾篇相關文章,甚至發佈了並回答here,但似乎我無法解決問題。java.lang.IllegalStateException:試圖重新查詢一個已經關閉的光標android.database.sqlite.SQLiteCursor @

我有3個活動: ACT1(主) ACT2 ACT3

當來回Act1-> ACT2和Act2-> ACT1我沒有得到任何問題 當去Act2-> ACT3我沒有得到任何問題 什麼時候去Act3-> Act2我偶爾會碰到以下錯誤:java.lang.IllegalStateException: trying to requery an already closed cursor [email protected]。這是一個ListView遊標。

我的嘗試: 1. ACT2的添加stopManagingCursor(currentCursor);到的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; 遊標比賽StocksCursor = 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我用下面的意圖,並希望沒有結果:

    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。

    回答

    1

    我稱之爲二維問題:導致這次崩潰的兩件事: 1.我用startManagingCursor(mItemCursor);我不應該有。 2.我忘了initCursorAdapter()(用於自動完成)上onResume()

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

    現在似乎很好地工作。但願如此...

    +0

    你能幫我類似的問題,我在這裏提出:http://stackoverflow.com/questions/11893727/Android的列表視圖與 - simplecursoradapter - 崩潰 - 的onResume – tiptopjat

    1

    將這個它可能爲你工作:

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

    這也適用

     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
          startManagingCursor(Cursor); 
         } 
    
    相關問題