保存电路板的状态以进行定位

时间:2013-03-15 21:39:52

标签: android savestate

嗨,我是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();
}

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

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

类型

试试这个:

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

答案 1 :(得分:0)

尝试

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

}