“Duplicate id, tag null, or parent id 0x0 with another fragment” when adding second Fragment to ActionBarActivity

青春壹個敷衍的年華 提交于 2019-12-13 03:35:17

问题


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 new Fragment in my main ActionBarActivity.

The main error is a android.view.InflateException: Binary XML file line #3: Error inflating class fragment error, on the return line in my PlaceDetailsFragment class.

Goal: To get a second Fragment on top the first SupportMapFragment, covering it for about 30%.

main.java

public class Main extends ActionBarActivity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        if (findViewById(R.id.fragment_container) != null) {

            if (savedInstanceState != null) {
                return;
            }

            FragmentTransaction mTransaction = getSupportFragmentManager()
                    .beginTransaction();
            SupportMapFragment mapFragmentSupportClass = new MapFragmentClass();
            mTransaction.add(R.id.fragment_container, mapFragmentSupportClass);
            mTransaction.commit();

            FragmentTransaction xTransaction = getSupportFragmentManager().beginTransaction();
            PlaceDetailsFragment placeDetailsFragment = new PlaceDetailsFragment();
            xTransaction.add(R.id.fragment_container, placeDetailsFragment);
            xTransaction.commit();
        }
    }

PlaceDetailsFragment.java This is where the app crashes, on the return line.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PlaceDetailsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.details, container, false);

    }
}

main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent" >

 </FrameLayout>

mapfragment.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
/>

details.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:id="@+id/details"
          android:name="com.qstudios.whatsopenlate.PlaceDetailsFragment"
          android:tag="detailsTag"
/>

To the best of my knowledge, I'm not using duplicate id's nor tags, so that leaves the parent id the fragment is sharing with mapFragment, main's fragment_container. Why is this a problem. Isn't it logical a parent has the same ID? Also, this is not a NestedFragment, correct? I'm using an ActionBarActivity and a FrameLayout to put the fragments in.

This error has halted my productivity with 2 hours already, and all of the answers I found on google were either about ActionBarSherlock, Nested Fragments, etc. None could provide a simple explanation or example on how to appropriately put 2 fragments in a FrameLayout. Even https://developer.android.com/training/basics/fragments/fragment-ui.html doesn't explain how to do this.

Anybody any experience with this, and knows what I'm doing wrong and how I could fix it?

Thanks in advance!


回答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:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android: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:

FragmentTransaction xTransaction = getSupportFragmentManager().beginTransaction();
PlaceDetailsFragment placeDetailsFragment = new PlaceDetailsFragment();
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.



来源:https://stackoverflow.com/questions/22702967/duplicate-id-tag-null-or-parent-id-0x0-with-another-fragment-when-adding-sec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!