jfeinstein10/SlidingMenu · error · IllegalStateException
enableSlidingActionBar must be called in onCreate.
Error message
enableSlidingActionBar must be called in onCreate.
What it means
setSlidingActionBarEnabled() must be called during onCreate, before SlidingActivityHelper.onPostCreate() attaches the SlidingMenu to the Activity. Once mOnPostCreateCalled is true the menu is already built, so changing the ActionBar-sliding flag throws IllegalStateException.
Solutions
- Move the setSlidingActionBarEnabled(true/false) call into onCreate (before onPostCreate)
- If runtime toggling is needed, recreate the activity or create a fresh SlidingMenu instance
- Use a field set before onCreate completes and pass it to setSlidingActionBarEnabled in onCreate
Example fix
// before
protected void onResume() { super.onResume(); setSlidingActionBarEnabled(true); } // throws
// after
protected void onCreate(Bundle b) {
super.onCreate(b);
setSlidingActionBarEnabled(true);
setBehindContentView(R.layout.menu_frame);
} Defensive patterns
Strategy: validation
Validate before calling
// call only inside onCreate, before onPostComplete:
@Override protected void onCreate(Bundle b) { super.onCreate(b); setSlidingActionBarEnabled(true); setBehindContentView(R.layout.menu_frame); } Try / catch
try { setSlidingActionBarEnabled(enabled); } catch (IllegalStateException e) { // too late; recreate activity to apply } Prevention
- Invoke setSlidingActionBarEnabled only in onCreate
- Never toggle it from onResume/click handlers
- Store the desired flag and apply it during onCreate on activity recreation
When it happens
Trigger: Calling setSlidingActionBarEnabled() in onResume, onStart, onPostCreate, or any point after onPostCreate has run; calling it after configuration change outside onCreate.
Common situations: Toggling the sliding ActionBar in response to user action at runtime; moving the call out of onCreate during a refactor; calling it in a fragment's lifecycle callback.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- This SlidingMenu appears to already be attached
- Both setBehindContentView must be called in onCreate in…
- The BehindFadeDegree must be between 0.0f and 1.0f
- Cannot set both behindOffset and behindWidth for a…
- slideStyle must be either SLIDING_WINDOW or SLIDING_CONTENT
AI-assisted analysis of jfeinstein10/SlidingMenu@4254feca3e (2026-09-09).
Data as JSON: /api/errors/d88bece9a9490775.
Report an issue: GitHub.
Appendix: source
Thrown at library/src/com/jeremyfeinstein/slidingmenu/lib/app/SlidingActivityHelper.java:97
mSlidingMenu.showMenu(false);
}
} else {
mSlidingMenu.showContent(false);
}
}
});
}
/**
* Controls whether the ActionBar slides along with the above view when the menu is opened,
* or if it stays in place.
*
* @param slidingActionBarEnabled True if you want the ActionBar to slide along with the SlidingMenu,
* false if you want the ActionBar to stay in place
*/
public void setSlidingActionBarEnabled(boolean slidingActionBarEnabled) {
if (mOnPostCreateCalled)
throw new IllegalStateException("enableSlidingActionBar must be called in onCreate.");
mEnableSlide = slidingActionBarEnabled;
}
/**
* Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
*
* @param id the resource id of the desired view
* @return The view if found or null otherwise.
*/
public View findViewById(int id) {
View v;
if (mSlidingMenu != null) {
v = mSlidingMenu.findViewById(id);
if (v != null)
return v;
}
return null;
}View on GitHub (pinned to 4254feca3e)