0

我有一个名为 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?

4

2 回答 2

0

You should check if Activity exists and if isAdded() is 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); 
    }}
于 2015-04-26T00:28:56.247 回答
0

我使用片段管理器按标签查找片段。如果它返回 null 我在创建片段时遇到问题。这取决于我的片段是否有有效的容器,是否调用了适配器并且适配器不满意,或者它是否不正确地调用了视图,因此请仔细检查所有内容。确保您的容器已添加到您的 ViewGroup(如果您正在使用),并且该容器已与任何父级分离。

我以编程方式创建我的片段并设置可以搜索的标签名称。

使用调试,确保片段构造函数被调用,然后确保 onCreateView 被调用。如果是这样,请单步执行 onCreateView 并查找会爆炸的行。确保片段返回一个视图。

还要确定您使用的是什么类型的片段,android 或 android.support.v4.app.Fragment。

有时我不得不使用 FragmentManager whatever_your_name_is.executePendingTransactions 来确保在访问之前已经添加了片段。请务必使用启动片段的片段管理器。

这是我的脑残,祝你好运。

于 2015-04-26T07:05:14.720 回答