Unmerciful
Unmerciful

Reputation: 1335

Caused by: java.lang.IllegalArgumentException multiple fragments

I speend a lot of time at this issue, I don't know where is problem.

I have got Activity with Fragment A.

Fragment A have got second Fragment B and B have got Fragment C.

Activity: layout -> with R.id.container (place where put next fragment)

A: layout -> with R.id.place_promo

B: layout -> with R.id.container_promo

C: layout have Linear Layout R.id.promo_container

And I Have got this error:

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060109 (pl.xx.xx.xx:id/container_promo) for fragment FragmentxxxxOne{4252e858 #3 id=0x7f060109}

All fragment I put in the same way:

getActivity()
        .getSupportFragmentManager()
        .beginTransaction()
        .replace(container, fragment)
        .commit();

Any sugestions?

I don't know when this error appear, because it is from my Error Handler.

Upvotes: 1

Views: 1687

Answers (2)

Srikanth K
Srikanth K

Reputation: 190

This will work fine, use this in onCreateView() method

View rootView;


if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }
        try {
            rootView = inflater.inflate(R.layout.myride, container, false);
        } catch (InflateException e) {

        }

Upvotes: 1

FrancescoC
FrancescoC

Reputation: 1058

public abstract FragmentTransaction replace (int containerViewId, Fragment fragment)

Calls replace(int, Fragment, String) with a null tag.

And from here ..

public abstract FragmentTransaction replace (int containerViewId, Fragment fragment, String tag)

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

Parameters

  • containerViewId Identifier of the container whose fragment(s) are to be replaced.
  • fragment The new fragment to place in the container.
  • tag Optional tag name for the fragment, to later retrieve the fragment with FragmentManager.findFragmentByTag(String).

So you are replacing in container the given fragment instance.

You also say every fragment is managed in this way.

The idea of how your app is working is :

  1. get fragment's ID. Let's call it "fragID"
  2. go to container view, based on container
  3. find fragment to be replaced, based on "fragId"
  4. remove the fragment found
  5. add the new fragment item

Problem is that as you just keep REPLACING fragments, you always expect to have one fragment already placed. On the first run when there is no fragment at all, replace method fails to find (point #3) the given fragment.

You should look forward to another method to add fragments in first place, and only if you ever need it you should replace them, checking their existence beforehand ( tip: use FragmentManager's find method )

Update

Might have missed to say one thing that could keep messing things up. I don't think that using activity.getSupportFragmentManager() might be the correct way to nesting fragments. It IS THE CORRECT WAY for adding the first fragment to the activity tho. Once you need to nest the second in the first, you need to call the first fragment's FragmentManager

Otherwise you'd be attaching Fragments only to the Activity

Upvotes: 1

Related Questions