Skip to content Skip to sidebar Skip to footer

"duplicate Id, Tag Null, Or Parent Id 0x0 With Another Fragment" When Adding Second Fragment To Actionbaractivity

I am getting a 'java.lang.IllegalArgumentException: Binary XML file line #3: Duplicate id 0x7f05003f, tag null, or parent id 0x0 with another fragment' errror when I try to add a n

Solution 1:

The problem, as you've more or less discovered yourself, is in the way the <fragment> tag is used in details.xml. Per the Android documentation:

An activity's layout XML can include <fragment> tags to embed fragment instances inside of the layout.

Basically, you were trying to use the tag as an actual layout for your fragment, whereas all it's meant to do is embed an existing fragment (which already has its own layout) in some other view. Therefore, the proper solution is indeed to build a layout for the fragment (e.g. a RelativeLayout as mentioned in the comments), making no use of <fragment>.

If you then want to add the fragment you've created to some other layout (such as main.xml), you can use <fragment> to reference it. For instance, main.xml could be rewritten as:

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/fragment_container"android:layout_width="match_parent"android:layout_height="match_parent" ><fragmentandroid:layout_width="match_parent"android:layout_height="200dp"android:id="@+id/details"android:name="com.qstudios.whatsopenlate.PlaceDetailsFragment"android:tag="detailsTag" /></FrameLayout>

In which case you could remove the following part from main.java:

FragmentTransactionxTransaction= getSupportFragmentManager().beginTransaction();
PlaceDetailsFragmentplaceDetailsFragment=newPlaceDetailsFragment();
xTransaction.add(R.id.fragment_container, placeDetailsFragment);
xTransaction.commit();

If, however, you choose to add the fragment programatically with the FragmentManager (as you have), there's no need to use the tag at all.

This is all explained very well in the Fragments API Guide (specifically the part about adding a fragment to an activity), which I'd strongly suggest reading.

Post a Comment for ""duplicate Id, Tag Null, Or Parent Id 0x0 With Another Fragment" When Adding Second Fragment To Actionbaractivity"