Suhel Chakraborty
Suhel Chakraborty

Reputation: 499

Load fragment inside DrawerLayout

I have created a DrawerLayout with first child as a FrameLayout and the second child as a ListView which will be the drawer.

Now in the activity, I have used the support fragment manager to load a fragment in the FrameLayout in the DrawerLayout.

The error code is

IllegalStateException: The specified child already has a parent. Use removeView() on the child

What can I do now?

Upvotes: 0

Views: 740

Answers (2)

Nativ
Nativ

Reputation: 3150

First of all, consider to support source code because without it it's a matter of guessing.

As the crash log states - you are trying to add a view that already has a parent view to another parent view, you can't do that, a view may have just a single ViewGroup parent view.

I'm guessing that in the instantiation of the fragment, when you creates a FragmentTransaction you're adding an id of a container view and after that trying to add the fragment to the FrameLayout.

The id you adding to FragmentTransaction must be the one of the FrameLayout.

For example:

FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.add(R.id.tabcontent, fragment);
trans.commitAllowingStateLoss();

the R.id.tabcontent should be the id of the intended fragment's container

Upvotes: 0

Illegal Argument
Illegal Argument

Reputation: 10338

How have you inflated your first fragment. It is usually caused by inflating wrong constructor so be sure to inflate the correct constructor.

View view= inflater.inflate(R.layout.fragment_first, container, false);
return view;

Upvotes: 1

Related Questions