0

嗨,我是 android 的新手,正在阅读 tictactoe 的教程。我需要保存电路板的状态,以便当我改变方向时,电路板的字符完好无损。这是代码片段 - 尝试和转折字符串正在保存和显示,但不是板上的 X 和 O。我不知道为什么

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);
    }

以下是tictactoe游戏返回棋盘状态的方法:

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

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

提前感谢您的帮助。

4

2 回答 2

1

我认为问题可能出在您的 setBoardGame 方法中。clone() 方法返回类型“Object”,但 mBoard 是类型“char[]”(字符数组)。因此,您需要从类型 Object 转换为类型 char[]

尝试这个:

public void setBoardState(char[] board) {
    mBoard = (char[]) board.clone();
}
于 2013-03-15T23:05:55.873 回答
0

尝试

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

}

于 2013-03-16T17:28:08.413 回答