2

関連する投稿をいくつか読んだり、ここに投稿して回答したりしましたが、問題を解決できなかったようです。

私には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カーソルです。

私が試したこと:stopManagingCursor(currentCursor);1。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(); カーソル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-> Act2Act2->Act1を移動しても問題は発生しません。そこで使用startActivityForResult(intent, ACTIVITY_EDIT);していますが、よくわかりません。これが私の問題の原因である可能性がありますか?

誰かがこのテーマに光を当てることができれば幸いです。私はこの主題についてもう少し学ぶことに興味があります。

ありがとう。

4

3 に答える 3

1

私はこれを2次元の問題と呼んでいます。このクラッシュの原因は2つあります。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();
}

今ではうまくいくようです。そうだといい...

于 2012-06-07T15:45:14.767 に答える
1

これを入れて、それはあなたのために働くかもしれません:

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

            orderCursor.requery();
    }
于 2013-06-24T05:52:15.853 に答える
0

これも機能します

       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            startManagingCursor(Cursor);
        }
于 2013-09-09T14:30:16.910 に答える