我有一个名为 Chessboard 的主要活动。在 Chessboard 的 onCreate 中,我设置了一个片段活动 GameInfoFragment。在此之后,调用查询 SQLite 数据库,之后立即调用 SQLiteDataInterpreter 类中的方法。从这里,调用 GameInfoFragment 类中的方法,其中应该填充我在 Chessboard 的 onCreate 中设置的片段中的 imageViews。
当我尝试启动 Chessboard 活动时,我收到以下错误:
Caused by: java.lang.IllegalStateException: Fragment GameInfoFragment{2ef389d9 id=0x7f09008d InfoTest} not attached to Activity
Chessboard 的 onCreate 中的代码如下:
FragmentManager manager = this.getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (chessboard.moveCounter % 2 == 0) {
transaction.add(com.zlaporta.chessgame.R.id.game_play_screen, gameInfo, "InfoTest");
} else {
transaction.add(com.zlaporta.chessgame.R.id.game_play_black_screen, gameInfo, "InfoTestBlack");
}
transaction.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("Current Game");
setSupportActionBar(toolbar);
DBAdapter boardstateDB = new DBAdapter(chessboard.this);
boardstateDB.open();
Cursor cur = boardstateDB.getAllRows();
if (cur != null && cur.moveToFirst()) {
cur.moveToLast();
String pieceData = cur.getString(cur.getColumnIndex("piecepositions"));
whiteKingMoved = cur.getInt(cur.getColumnIndex("whitekingmoved"));
blackKingMoved = cur.getInt(cur.getColumnIndex("blackkingmoved"));
leftWRMoved = cur.getInt(cur.getColumnIndex("whiterook1moved"));
rightWRMoved = cur.getInt(cur.getColumnIndex("whiterook2moved"));
leftBRMoved = cur.getInt(cur.getColumnIndex("blackrook1moved"));
rightBRMoved = cur.getInt(cur.getColumnIndex("blackrook2moved"));
moveCounter = cur.getInt(cur.getColumnIndex("movecount"));
String madeMoves = cur.getString(cur.getColumnIndex("mademoves"));
storedTakenPieces = cur.getString(cur.getColumnIndex("takenpieces"));
SQLiteDataInterpreter boardRefresher = new SQLiteDataInterpreter();
boardRefresher.refreshBoard(pieceData, madeMoves, chessboard.this, storedTakenPieces);
} else {
boardstateDB.updateGameState("r00 k01 b02 q03 a04 b05 k06 r07 p10 p11 p12 p13 p14 p15 p16 p17 P60 P61 P62 P63 P64 P65 P66 P67 R70 K71 B72 Q73 A74 B75 K76 R77",
0, 0, 0, 0, 0, 0, 0, " ", " ");
Cursor curs = boardstateDB.getAllRows();
curs.moveToLast();
String pieceData = curs.getString(curs.getColumnIndex("piecepositions"));
whiteKingMoved = curs.getInt(curs.getColumnIndex("whitekingmoved"));
blackKingMoved = curs.getInt(curs.getColumnIndex("blackkingmoved"));
leftWRMoved = curs.getInt(curs.getColumnIndex("whiterook1moved"));
rightWRMoved = curs.getInt(curs.getColumnIndex("whiterook2moved"));
leftBRMoved = curs.getInt(curs.getColumnIndex("blackrook1moved"));
rightBRMoved = curs.getInt(curs.getColumnIndex("blackrook2moved"));
moveCounter = curs.getInt(curs.getColumnIndex("movecount"));
String madeMoves = curs.getString(curs.getColumnIndex("mademoves"));
storedTakenPieces = curs.getString(curs.getColumnIndex("takenpieces"));
SQLiteDataInterpreter boardRefresher = new SQLiteDataInterpreter();
boardRefresher.refreshBoard(pieceData, madeMoves, chessboard.this, storedTakenPieces);
以及导致问题的我的 GameInfoFragment 类中的相关代码:
public void setTakenPiece(String[] takenpiecerecord) {
takenPiece = takenpiecerecord;
int c;
for (c = 0; c < takenPiece.length; c++) {
String takenPieceHolder = "t" + c;
String pieceCheck = takenPiece[c];
//System.out.println(square);
int takenPieceHolderID = this.getResources().getIdentifier(takenPieceHolder, "id", getActivity().getPackageName());
ImageView takenPieceHolderSquare = (ImageView) v.findViewById(takenPieceHolderID);
导致问题的主要代码行是我尝试在上面的代码中设置takePieceHolderID 的值。
如何确保添加我的片段,以便在我的 Chessboard Activity 开始时填充其 imageViews?