You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android屏幕旋转时调用onRestoreInstanceState设置玩家回合遇空指针异常

Fixing NullPointerException on Screen Rotation in Your Socket.io Tic-Tac-Toe Game

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

  1. Lifecycle Timing Mismatch
    When you rotate the screen, Android destroys your current Activity/Fragment and recreates it from scratch. If you're initializing player1 later in the lifecycle (like onStart() or onResume()) but trying to restore the turn state in onRestoreInstanceState() or early in onCreate(), player1 will still be null when you call setMyTurn().

  2. Unchecked Nulls in State Saving
    If player1 was null when onSaveInstanceState() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:51:10