Android 尝试使用 onRestoreInstanceState 设置玩家回合时出现空指针异常

Android null pointer exception when trying to set player turn using onRestoreInstanceState

我正在做一个简单的多人 socket.io 井字游戏。到目前为止一切正常,但是,当我尝试旋转屏幕时出现空指针异常错误。我有两个覆盖方法来保存和恢复我的玩家回合的布尔值的值。

当我尝试像这样直接设置值时 player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")) 它给了我一个空指针异常。

有人知道为什么吗?

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putBoolean("playerTurn", player1.isMyTurn());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")); //this seems to cause the issue
}

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.twochicken.multiplayertictactoe, PID: 8612
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twochicken.multiplayertictactoe/com.twochicken.multiplayertictactoe.MainActivity}: java.lang.NullPointerException
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
                  at android.app.ActivityThread.access0(ActivityThread.java:135)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5017)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.NullPointerException
                  at com.twochicken.multiplayertictactoe.MainActivity.onRestoreInstanceState(MainActivity.java:416)
                  at android.app.Activity.performRestoreInstanceState(Activity.java:916)
                  at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738) 
                  at android.app.ActivityThread.access0(ActivityThread.java:135) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5017) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                  at dalvik.system.NativeStart.main(Native Method) 

尝试将第一种方法中的行反转

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putBoolean("playerTurn", player1.isMyTurn());
    super.onSaveInstanceState(savedInstanceState);
}

其次确保您在使用数据之前拥有数据。

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
     if (savedInstanceState != null) {       
          player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")); 
     }
}

我给你的一个提示是创建一个常量而不是 "playerTurn",这可以使你的代码更容易维护