Android屏幕旋转时调用onRestoreInstanceState设置玩家回合遇空指针异常
Hey there, let's break down why you're hitting that NullPointerException when rotating the screen in your multiplayer Tic-Tac-Toe app. The issue with player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")) almost always comes down to one core problem: the player1 object isn't initialized yet when you try to call its method after a screen rotation.
Common Root Causes
Lifecycle Timing Mismatch
When you rotate the screen, Android destroys your current Activity/Fragment and recreates it from scratch. If you're initializingplayer1later in the lifecycle (likeonStart()oronResume()) but trying to restore the turn state inonRestoreInstanceState()or early inonCreate(),player1will still be null when you callsetMyTurn().Unchecked Nulls in State Saving
Ifplayer1was null whenonSaveInstanceState()ran, you might be saving an invalid value (or nothing at all)—while this usually leads to a different issue, it's worth verifying to rule it out.
Step-by-Step Solutions
1. Initialize player1 Before Restoring State
The safest fix is to create your player1 instance before attempting to restore its turn state. Do this early in the onCreate() method:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_game_layout); // First, initialize the player1 object with your required parameters player1 = new Player(/* add constructor args here */); // Then restore the turn state if savedInstanceState exists if (savedInstanceState != null) { player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")); } // Rest of your setup (Socket.io connections, UI bindings, etc.) }
2. Add Defensive Null Checks (As a Backup)
If you need to handle state restoration specifically in onRestoreInstanceState(), add a null check to avoid the crash:
@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); // Only proceed if player1 is already initialized if (player1 != null && savedInstanceState != null) { player1.setMyTurn(savedInstanceState.getBoolean("playerTurn")); } }
Note: This is a defensive measure—always prioritize fixing the root cause by initializing player1 early.
3. Verify Your State Saving Code
Double-check that you're correctly saving the turn state when the Activity is about to be destroyed:
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Ensure player1 isn't null before saving its state if (player1 != null) { outState.putBoolean("playerTurn", player1.isMyTurn()); } }
Bonus: Socket.io Configuration Tip
Since you're using Socket.io for multiplayer, remember that screen rotation will reset your Socket connection. Consider using a ViewModel to persist the connection state across configuration changes, or handle reconnection explicitly in onCreate()—this will prevent other unexpected multiplayer-related issues down the line.
内容的提问来源于stack exchange,提问作者tiger_groove

