1

在我正在创建的井字游戏应用程序中,我设置了一个替代屏幕视图来处理方向的变化。

我的主 Java 文件中的 onCreate() 方法如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mInfoTextView = (TextView) findViewById(R.id.information); 
    mHumanScoreTextView = (TextView) findViewById(R.id.player_score);
    mComputerScoreTextView = (TextView) findViewById(R.id.computer_score);
    mTieScoreTextView = (TextView) findViewById(R.id.tie_score);

    mGame = new TicTacToeGame();

    mBoardView = (BoardView) findViewById(R.id.board); 
    mBoardView.setGame(mGame); 
    mBoardView.setOnTouchListener(mTouchListener); 

    if (savedInstanceState == null)
        startNewGame();
    else {
        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");
        mFirstMove = savedInstanceState.getChar("mFirstMove");
    }
    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);
    outState.putChar("mFirstMove", mFirstMove);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    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");
    mFirstMove = savedInstanceState.getChar("mFirstMove");
}

一切都很好,直到我对游戏进行了重新编程,这样棋盘上就不再出现 X 和 O 字符,而是出现了代表 Xs 和 Os 的图片。这样做需要我实现一个 TouchListener。游戏在垂直模式下完美运行,但一旦方向改变,它就会立即崩溃。我什至看不到翻转屏幕的变化。如果我在 layout-land main.xml 文件中查看水平布局,它看起来很好。当它实际运行而应用程序崩溃时,我无法接近访问它。

我是一个新手,所以不知道是什么原因造成的,也不知道要发布哪些相关信息以获得更多帮助。有人有建议吗?

编辑 - 这是 LogCat 错误日志:虽然程序在实际设备上的方向更改时崩溃,但在模拟器中它会在程序运行时立即崩溃

03-22 00:27:31.739: E/Trace(1042): error opening trace file: No such file or directory (2)
03-22 00:27:31.739: W/Trace(1042): Unexpected value from nativeGetEnabledTags: 0
03-22 00:27:31.739: W/Trace(1042): Unexpected value from nativeGetEnabledTags: 0
03-22 00:27:31.749: W/Trace(1042): Unexpected value from nativeGetEnabledTags: 0
03-22 00:27:31.789: W/Trace(1042): Unexpected value from nativeGetEnabledTags: 0
03-22 00:27:31.789: W/Trace(1042): Unexpected value from nativeGetEnabledTags: 0
03-22 00:27:31.909: D/AndroidRuntime(1042): Shutting down VM
03-22 00:27:31.909: W/dalvikvm(1042): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-22 00:27:31.920: E/AndroidRuntime(1042): FATAL EXCEPTION: main
03-22 00:27:31.920: E/AndroidRuntime(1042): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.joe.tictactoe/edu.joe.tictactoe.AndroidTicTacToeActivity}: java.lang.NullPointerException
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at  android.os.Looper.loop(Looper.java:137)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at java.lang.reflect.Method.invoke(Method.java:511)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at dalvik.system.NativeStart.main(Native Method)
03-22 00:27:31.920: E/AndroidRuntime(1042): Caused by: java.lang.NullPointerException
03-22 00:27:31.920: E/AndroidRuntime(1042):     at edu.joe.tictactoe.AndroidTicTacToeActivity.resetButtons(AndroidTicTacToeActivity.java:194)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at edu.joe.tictactoe.AndroidTicTacToeActivity.displayScores(AndroidTicTacToeActivity.java:311)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at edu.joe.tictactoe.AndroidTicTacToeActivity.onCreate(AndroidTicTacToeActivity.java:93)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.Activity.performCreate(Activity.java:5104)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-22 00:27:31.920: E/AndroidRuntime(1042):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-22 00:27:31.920: E/AndroidRuntime(1042):     ... 11 more
03-22 00:27:32.211: D/dalvikvm(1042): GC_CONCURRENT freed 351K, 17% free 2438K/2908K, paused 109ms+3ms, total 288ms

编辑 2: displayScores() 和 resetButtons() 代码:

192 public void resetButtons()
193 {
194     for (int i = 0; i < mBoardButtons.length; i++)
195     {
196         mBoardButtons[i].setText("");                       
197         mBoardButtons[i].setEnabled(true);              
198         mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
199     }
200 }

304 private void displayScores() {
305     mHumanScoreTextView.setText(Integer.toString(mHumanWins));
306     mComputerScoreTextView.setText(Integer.toString(mComputerWins));
307     mTieScoreTextView.setText(Integer.toString(mTies));
308
309
310     char [] temp = mGame.getBoardState();
311     resetButtons();
312
313     for (int i = 0; i < TicTacToeGame.BOARD_SIZE; i++)
314         if (temp [i] != TicTacToeGame.OPEN_SPOT) setMove(temp [i], i);
315 }

编辑 3:问题已解决 最初,电路板使用的按钮填充有字符 x 或 o。我删除了这些以使用图标,但忘记删除 resetButtons() 行和代码。它试图重置不再存在的东西。

4

1 回答 1

1

感谢所有在评论中提供帮助的人。否则我不知道该去哪里看。

最初,棋盘使用在 onCreate() 中初始化的按钮,并随着游戏的进行填充字符 x 或 o。

我删除了这些按钮,绘制了一个网格并使用坐标来放置图片来表示 x 和 o。但是,我忘记删除 resetButtons() 的命令,这意味着当应用程序启动时,它试图清除一些不存在的东西。删除此代码后,一切正常。

于 2013-03-22T00:46:27.253 回答