我有一个名为Chessboard的主要活动。在Chessboard中,在onCreate中,我设置了一个片段活动GameInfoFragment。在此之后,调用SQLite数据库,之后立即调用SQLiteDataInterpreter类中的方法。从这里开始,调用GameInfoFragment类中的方法,其中应填充我在Chessboard&on; 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);
导致问题的主要代码是我尝试在上面的代码中设置takenPieceHolderID的值。
如何确保添加我的片段,以便在我的棋盘活动开始时填充其imageViews?
答案 0 :(得分:0)
您应该检查活动是否存在以及isAdded()
是True
public void setTakenPiece(String[] takenpiecerecord) {
activity = getActivity();
if (isAdded() && activity) {
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);
}}
答案 1 :(得分:0)
我使用片段管理器按标签查找片段。如果它返回null我有片段创建的问题。这取决于我的片段是否具有有效容器,调用适配器和适配器是否不满意,或者如果它不正确地调用视图,所以请仔细检查所有。确保您的容器已添加到ViewGroup中(如果您使用的是容器),并且该容器已从任何父容器中分离。
我以编程方式创建我的片段并设置可以搜索的标记名称。
使用debug,确保调用片段构造函数,然后确保调用onCreateView。如果是这样,单步执行onCreateView并查找炸弹线。确保片段返回视图。
还要确定您使用的是哪种类型的片段,android或android.support.v4.app.Fragment。
有时我不得不使用FragmentManager whatever_your_name_is.executePendingTransactions来确保在访问之前添加了片段。请务必使用启动片段的片段管理器。
那是我的脑筋,祝你好运。