This SlidingMenu appears to already be attached Android

349 Views Asked by At

I getting following error

This SlidingMenu appears to already be attached

This my source code

SlidingMenu menu = new SlidingMenu(this, SlidingMenu.SLIDING_WINDOW);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.menu);

I have used sliding menu library by jfeinstein

I want to slide menu with actionbar. Please help me.

2

There are 2 best solutions below

0
On

I met the same question. There is a R.java file in com.jeremyfeinstein.slidingmenu.lib,you might miss it. You can copy it from other SlidingMenu project into gen folder.

0
On

Problem

From the SlidingMenu.java in attachToActivity method

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

here getParent is not null because you already assigned SlidingMenu.SLIDING_WINDOW when you create object of SlidingMenu.

e.g. SlidingMenu menu = new SlidingMenu(this, SlidingMenu.SLIDING_WINDOW);

above line of code calls attachToActivity. see in code

public SlidingMenu(Activity activity, int slideStyle) {
    this(activity, null);
    this.attachToActivity(activity, slideStyle);
}

So you are adding menu to layout twice. And that is the cause of problem.

Solution use one of the following.

  1. Use below constructor

    SlidingMenu menu = new SlidingMenu(this);

OR

  1. remove menu.attachToActivity(); line