我正在做一个简单的多人socket.io tic tac toe游戏。到目前为止一切正常,但是,当我尝试旋转屏幕时,我得到一个空指针异常错误。我有两个覆盖方法来保存和恢复我的播放器turn的布尔值的值。
当我尝试直接设置这样的值时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.access$900(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.access$900(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)
答案 0 :(得分:1)
尝试在第一种方法中反转行
@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"这可以使你的代码更容易维护