jfeinstein10/SlidingMenu · error · IllegalStateException

This SlidingMenu appears to already be attached

Error message

This SlidingMenu appears to already be attached

What it means

A SlidingMenu instance can only be attached to one Activity once. attachToActivity() checks getParent() and throws IllegalStateException if the view already has a parent, i.e. it was already attached (or added to a layout) previously.

Solutions

  1. Call attachToActivity only once per SlidingMenu instance, per Activity lifecycle
  2. Create a new SlidingMenu instance before re-attaching (e.g. on configuration change)
  3. Remove the SlidingMenu from its parent (getParent().removeView(...)) only if deliberate re-parenting is intended
  4. If using SlidingActivity/SlidingFragmentActivity, do not also call attachToActivity manually - the helper does it

Example fix

// before
protected void onCreate(Bundle b) { menu.attachToActivity(this, SLIDING_CONTENT); }
protected void onPostCreate(Bundle b) { menu.attachToActivity(this, SLIDING_CONTENT); } // throws
// after
protected void onPostCreate(Bundle b) { menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); } // once
Defensive patterns

Strategy: validation

Validate before calling

if (menu.getParent() == null) { menu.attachToActivity(activity, SlidingMenu.SLIDING_CONTENT); }

Try / catch

try { menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); } catch (IllegalStateException e) { // already attached; reuse existing menu }

Prevention

When it happens

Trigger: Calling attachToActivity() twice on the same SlidingMenu object, e.g. in both onCreate and onPostCreate, or after re-creating/re-attaching the activity; also if the view was inflated directly into a layout.

Common situations: Re-attaching after configuration change without recreating the SlidingMenu; calling attachToActivity in both Activity.onCreate and onPostCreate; library misuse where SlidingMenu was declared in XML (already parented) and then attached programmatically.

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


AI-assisted analysis of jfeinstein10/SlidingMenu@4254feca3e (2026-09-09). Data as JSON: /api/errors/12249109dde995d5. Report an issue: GitHub.

Appendix: source

Thrown at library/src/com/jeremyfeinstein/slidingmenu/lib/SlidingMenu.java:307

	 * @param slideStyle either SLIDING_CONTENT or SLIDING_WINDOW
	 */
	public void attachToActivity(Activity activity, int slideStyle) {
		attachToActivity(activity, slideStyle, false);
	}

	/**
	 * Attaches the SlidingMenu to an entire Activity
	 * 
	 * @param activity the Activity
	 * @param slideStyle either SLIDING_CONTENT or SLIDING_WINDOW
	 * @param actionbarOverlay whether or not the ActionBar is overlaid
	 */
	public void attachToActivity(Activity activity, int slideStyle, boolean actionbarOverlay) {
		if (slideStyle != SLIDING_WINDOW && slideStyle != SLIDING_CONTENT)
			throw new IllegalArgumentException("slideStyle must be either SLIDING_WINDOW or SLIDING_CONTENT");

		if (getParent() != null)
			throw new IllegalStateException("This SlidingMenu appears to already be attached");

		// get the window background
		TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
		int background = a.getResourceId(0, 0);
		a.recycle();

		switch (slideStyle) {
		case SLIDING_WINDOW:
			mActionbarOverlay = false;
			ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
			ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
			// save ActionBar themes that have transparent assets
			decorChild.setBackgroundResource(background);
			decor.removeView(decorChild);
			decor.addView(this);
			setContent(decorChild);
			break;
		case SLIDING_CONTENT:

View on GitHub (pinned to 4254feca3e)