Android 应用的屏幕旋转不断崩溃

Screen Rotation for Android app keeps crashing

我制作了一个井字游戏应用程序,并且我想能够允许该应用程序在不重新启动应用程序的情况下旋转。我在 class 中学习了如何进行屏幕旋转,并且我编写了一个 OnSaveInstanceState 方法,但是我的应用程序一直崩溃。它在没有这种方法的情况下运行,所以我知道它有问题但不确定。

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

尝试

 if(savedInstanceState != null){
        mHumanCounter = savedInstanceState.getInt(GAME_INDEX, 0);
        mTieCounter = savedInstanceState.getInt(GAME_INDEX, 0);
        mAndroidCounter = savedInstanceState.getInt(GAME_INDEX, 0);
  }

你应该始终将你的陈述放在括号中,即使它是一行。这是一个很好的做法。

删除

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

 if(savedInstanceState != null)
        mHumanCounter = savedInstanceState.getInt(GAME_INDEX, 0);
        mTieCounter = savedInstanceState.getInt(GAME_INDEX, 0);
        mAndroidCounter = savedInstanceState.getInt(GAME_INDEX, 0);

只需将此添加到您的清单中

 <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize"/>

应该可以。