関連する投稿をいくつか読んだり、ここに投稿して回答したりしましたが、問題を解決できなかったようです。
私には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);
}
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);
していますが、よくわかりません。これが私の問題の原因である可能性がありますか?
誰かがこのテーマに光を当てることができれば幸いです。私はこの主題についてもう少し学ぶことに興味があります。
ありがとう。