方向的改变导致应用程序崩溃

时间:2013-03-22 00:20:28

标签: android orientation

在我创建的Tic-Tac-Toe应用程序中,我设置了一个替代屏幕视图来处理方向的变化。

我的主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字符出现在棋盘上,代表X和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()行和代码。它试图重置不再存在的东西。

1 个答案:

答案 0 :(得分:1)

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

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

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