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

Android屏幕旋转时布尔值存取异常致应用崩溃求助

Hey there! Let's get that screen rotation crash sorted out for your Android app. Chances are the issue is either a mistake in how you're saving/retrieving the boolean value from the Bundle, or you're trying to update your buttons before they've been initialized. Let's walk through the correct implementation step by step:

Step 1: Use a Constant for Your Bundle Key

First, define a static constant for your boolean's key to avoid typos (this is a common source of bugs!). Put this at the top of your Activity class:

private static final String KEY_BUTTON_ENABLED_STATE = "button_enabled_state";
Step 2: Save the Boolean Value When the Activity is Destroyed

Override onSaveInstanceState to store your boolean value before the screen rotation destroys the Activity. Make sure to call super.onSaveInstanceState first—this preserves the default Activity state:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // Grab the current enabled state of your buttons (or use a dedicated boolean variable if you have one)
    boolean isButtonsEnabled = mTrueButton.isEnabled();
    outState.putBoolean(KEY_BUTTON_ENABLED_STATE, isButtonsEnabled);
}
Step 3: Retrieve the Value and Update Buttons

You can restore the state either in onCreate or onRestoreInstanceState. The key rule here: always initialize your buttons before trying to update their state (otherwise you'll get a NullPointerException).

Option 1: Restore in onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout);

    // First, initialize your buttons!
    mTrueButton = findViewById(R.id.true_button);
    mFalseButton = findViewById(R.id.false_button);

    // Check if we have saved state to restore
    if (savedInstanceState != null) {
        // Retrieve the boolean—use a default value (here, true) in case the key is missing
        boolean isButtonsEnabled = savedInstanceState.getBoolean(KEY_BUTTON_ENABLED_STATE, true);
        // Update both buttons
        mTrueButton.setEnabled(isButtonsEnabled);
        mFalseButton.setEnabled(isButtonsEnabled);
    } else {
        // Set the default state for first launch
        mTrueButton.setEnabled(true);
        mFalseButton.setEnabled(true);
    }
}

Option 2: Restore in onRestoreInstanceState

If you prefer to keep state restoration separate from initialization, use this method instead. It's called after onStart, so your buttons will already be initialized:

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    boolean isButtonsEnabled = savedInstanceState.getBoolean(KEY_BUTTON_ENABLED_STATE, true);
    mTrueButton.setEnabled(isButtonsEnabled);
    mFalseButton.setEnabled(isButtonsEnabled);
}
Common Mistakes to Fix
  • Forgot to call super methods: Skipping super.onSaveInstanceState or super.onRestoreInstanceState breaks the Activity's state-saving lifecycle, leading to corrupt Bundle data.
  • Typos in bundle keys: Using different strings when saving and retrieving (e.g., "button_state" vs "Button_State") will cause the boolean to be missing from the Bundle. Using a constant eliminates this.
  • Accessing buttons before initialization: If you try to call setEnabled before findViewById, you'll get a NullPointerException—this is one of the most common causes of crashes here.
  • No default value in getBoolean: If the Bundle doesn't have your key (e.g., first launch), not providing a default value will throw an exception. Always include the second parameter in getBoolean().

内容的提问来源于stack exchange,提问作者Jose Pena Sanchez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:32:11