[英]Screen Rotation for Android app keeps crashing
I made a tic tac toe app and I'm suppose to be able to allow the app to rotate without it restarting the app. 我制作了一个井字游戏应用程序,我想能够允许该应用程序旋转而无需重新启动该应用程序。 I learned how to do screen rotation in class, and I have written an OnSaveInstanceState method, however my app keeps crashing.
我在课堂上学习了如何进行屏幕旋转,并编写了OnSaveInstanceState方法,但是我的应用程序始终崩溃。 It runs without this method, so I know something is wrong with it however not sure.
它在没有这种方法的情况下运行,因此我知道有些问题,但是不确定。
package com.example.johnavan.tictactoeassingment2;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "TicTacToeGame";
private static String GAME_INDEX = "game_index";
private TicTacToeGame mGame;
private Button mBoardButtons[];
private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;
private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;
private boolean mHumanFirst = true;
private boolean mGameOver = false;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(GAME_INDEX, mHumanCounter);
savedInstanceState.putInt(GAME_INDEX, mTieCounter);
savedInstanceState.putInt(GAME_INDEX, mAndroidCounter);
Log.i(TAG, "onSaveInstanceState(Bundle)");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null)
mHumanCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mTieCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mAndroidCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mBoardButtons = new Button[mGame.getBoardSize()];
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humanCount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeGame();
startNewGame();
}
private void startNewGame()
{
mGame.clearBoard();
for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.COMP_PLAYER, move);
mHumanFirst = true;
}
}
private class ButtonClickListener implements View.OnClickListener
{
int location;
public ButtonClickListener(int location)
{
this.location = location;
}
public void onClick(View view)
{
if (!mGameOver)
{
if (mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);
int winner = mGame.checkForWinner();
if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.COMP_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
startNewGame();
mGameOver = false;
}
else if (winner == 2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
startNewGame();
mGameOver = false;
}
else
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
startNewGame();
mGameOver = false;
}
}
}
}
}
private void setMove(char player, int location)
{
mGame.setMove(player, location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
mBoardButtons[location].setTextColor(Color.RED);
}
}
try 尝试
if(savedInstanceState != null){
mHumanCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mTieCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mAndroidCounter = savedInstanceState.getInt(GAME_INDEX, 0);
}
you should always put brackets around your statements even if its a one liner. 您应该始终在语句两边加括号,即使它是一个衬里也是如此。 Its a good practice.
这是一个好习惯。
Remove 去掉
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can save the view hierarchy
state
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(GAME_INDEX, mHumanCounter);
savedInstanceState.putInt(GAME_INDEX, mTieCounter);
savedInstanceState.putInt(GAME_INDEX, mAndroidCounter);
Log.i(TAG, "onSaveInstanceState(Bundle)");
}
And 和
if(savedInstanceState != null)
mHumanCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mTieCounter = savedInstanceState.getInt(GAME_INDEX, 0);
mAndroidCounter = savedInstanceState.getInt(GAME_INDEX, 0);
Just add This on your Manifest 只需在清单上添加
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"/>
It should work. 它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.