我正在建造tictactoe,并且我正在尝试让Java恢复旋转的木板。我很确定我已经设法使onsaveInstanceState正常工作,但是返回可序列化的对象真是麻烦。
有人可以告诉我我在做什么错吗。
这是我的mainActivity
Game game;
TextView[] gameMessageIm = new TextView[3];
String[] gameMessage = {"playerone", "playertwo", "draw", "invalid"};
TileState[][] mainboard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("mainboard", mainboard);
for (int i = 0; i < gameMessage.length; i++) {
outState.putInt(gameMessage[i], gameMessageIm[i].getVisibility());
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
initGameMessage();
super.onRestoreInstanceState(savedInstanceState);
game = (Game) savedInstanceState.getSerializable("mainboard");
for (int i = 0; i < gameMessage.length; i++){
gameMessageIm[i].setVisibility(savedInstanceState.getInt(gameMessage[i]));
}
}
这是游戏类 公共类游戏实现了可序列化{
final private int BOARD_SIZE = 3;
private TileState[][] board;
private GameState Gstate;
private Boolean playerOneTurn;
private int movesPlayed;
private Boolean gameOver;
// classes' constructor
public Game() {
board = new TileState[BOARD_SIZE][BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++)
for (int j = 0; j < BOARD_SIZE; j++)
board[i][j] = TileState.BLANK;
Gstate = GameState.IN_PROGRESS;
playerOneTurn = true;
gameOver = false;
}
public TileState[][] returnboard(){
return board;
}`