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:
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";
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); }
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); }
- Forgot to call super methods: Skipping
super.onSaveInstanceStateorsuper.onRestoreInstanceStatebreaks 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
setEnabledbeforefindViewById, you'll get aNullPointerException—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

