简体   繁体   English

保存板的状态以进行定向

[英]save the state of the board for orientation

Hi I am a newbie at android and going through a tutorial for tictactoe. 嗨,我是android的新手,正在学习tictactoe教程。 I need to save the state of the board so that when I change orientation the board appears with characters intact. 我需要保存电路板的状态,以便当我改变方向时,电路板会显示完整的字符。 Here are the pieces of code - The tries and turns strings are being saved and display but not the X's and O's on the board. 这是代码段-反复尝试字符串已保存并显示,但板上没有X和O。 I do not have a clue why 我不知道为什么

MainActivity.java MainActivity.java

           mGame = new TicTacToeGame();

       if (savedInstanceState == null) { 
            startNewGame();
            } 
            else { 
             // Restore the game's state                    
            mGame.setBoardState(savedInstanceState.getCharArray("board"));              
            mGameOver = savedInstanceState.getBoolean("mGameOver"); 
            mInfoTextView.setText(savedInstanceState.getCharSequence("info")); 
            mHumanWins = savedInstanceState.getInt("mHumanWins"); 
            mComputerWins = savedInstanceState.getInt("mComputerWins"); 
            mTies = savedInstanceState.getInt("mTies"); 
            mTurn = savedInstanceState.getChar("mTurn"); 
            } 
            displayScores(); 

       }

        @Override
    protected void onSaveInstanceState(Bundle outState) {
     super.onSaveInstanceState(outState);
     outState.putCharArray("board", mGame.getBoardState());
     outState.putBoolean("mGameOver", mGameOver);
     outState.putInt("mHumanWins",Integer.valueOf(mHumanWins));
     outState.putInt("mComputerWins",Integer.valueOf(mComputerWins));
     outState.putInt("mTies", Integer.valueOf(mTies));
     outState.putCharSequence("info", mInfoTextView.getText());
     outState.putChar("mTurn", mTurn);
    }

Here are the methods on the tictactoe game returning the state of the board: 以下是tictactoe游戏中返回棋盘状态的方法:

    public char[] getBoardState() {
    return mBoard;
}

public void setBoardState(char[] board) {
    mBoard = board.clone();
}

Thanks in advance for any help. 在此先感谢您的帮助。

I think the problem might be in your setBoardGame method. 我认为问题可能出在您的setBoardGame方法中。 The clone() method returns type "Object", but mBoard is type "char[]" (a character array). clone()方法返回类型“ Object”,但是mBoard类型为“ char []”(字符数组)。 Therefore, you need to cast from type Object to type char[] 因此,您需要从Object类型转换为char []类型

Try this: 尝试这个:

public void setBoardState(char[] board) {
    mBoard = (char[]) board.clone();
}

Try 尝试

public char[] getBoardState() {
return mBoard.clone();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM