Error inflating class fragment: Duplicate id , tag null, or parent id with another fragment

7

I am trying to make an app in which when user clicks on item in ListView, the GoogleMaps is displayed.

I have tried following code:

ShowPlacesActivity.java

public class ShowPlaceActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_place);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container_showplaces, new PlaceholderFragment())
                    .commit();
        }
        getActionBar().setSubtitle(getString(R.string.activity_showplaces_subtitle));
    }

}

activity_show_place.xml

<FrameLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_showplaces"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.krupal.rememberthisplace.ShowPlaceActivity" tools:ignore="MergeRootFrame" />

fragment_show_place.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"

    >


    <ListView
        android:id="@+id/listView_places"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      />


</LinearLayout>

fragment_maps.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/mymap"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

I want to show maps on listview's item click...so, I have implemented two fragments as follows:

In PlaceholderFragment.java, I have set onitemclicklistener as:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_show_place, container, false);



            final MySqliteHelper db = new MySqliteHelper(getActivity().getBaseContext());


            listview_places = (ListView)rootView.findViewById(R.id.listView_places);

            final PlacesAdapter places_adapter = new PlacesAdapter(getActivity(),R.layout.listview_item_row,places);


            listview_places.setAdapter(places_adapter);


            listview_places.setLongClickable(true);

            listview_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position,long id) {
                    Place place = places_adapter.getItem(position);
                    String dbvaluename = place.getname();

                  Place selectedplace =  db.getPlace(dbvaluename);


                    dblatitude = selectedplace.getlatitude();

                    dblongitude = selectedplace.getlongitude();


                    dbplacename = place.getname();

                   maps = new MapsFragment();
                    Bundle bundle = new Bundle();
                    bundle.putDouble("dbplacename",dblatitude);
                    bundle.putDouble("dbplacename",dblongitude);
                    bundle.putString("dbplacename",dbplacename);
                   maps.setArguments(bundle);
                    FragmentManager fm = getFragmentManager();

                    FragmentTransaction ft = fm.beginTransaction();

                    ft.replace(R.id.container_showplaces,maps);

                    ft.addToBackStack(null);

                    ft.commit();

                }



        });
}
}

and In MapsFragment.java, I have shown maps as:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


   rootView = inflater.inflate(R.layout.fragment_maps, container, false);


    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootView;
}

When I click first time on ListView's item, it shows the Map...Then, when I select another item, it crashes:

Logcat log:

android.view.InflateException: Binary XML file line #7: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) at android.app.BackStackRecord.run(BackStackRecord.java:622) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0b001e, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment at android.app.Activity.onCreateView(Activity.java:4248) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)             at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)             at android.view.LayoutInflater.inflate(LayoutInflater.java:489)             at android.view.LayoutInflater.inflate(LayoutInflater.java:396)             at com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40)             at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)             at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)             at android.app.BackStackRecord.run(BackStackRecord.java:622)             at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)             at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)             at android.os.Handler.handleCallback(Handler.java:605)             at android.os.Handler.dispatchMessage(Handler.java:92)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:4424)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)             at dalvik.system.NativeStart.main(Native Method)

EDIT: this soloution didn't work for me

android
xml
android-layout
listview
google-maps-android-api-2
asked on Stack Overflow Dec 21, 2014 by Fenil • edited May 23, 2017 by Community

3 Answers

22

I solved it now!

The another solution is linked answer worked for me well.

https://stackoverflow.com/a/14484640/3960528

As explained there, I added onDestroyView() in `fragment that contains maps.

@Override
public void onDestroyView() {
    super.onDestroyView();
    MapFragment f = (MapFragment) getFragmentManager()
                                         .findFragmentById(R.id.mymap);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}
answered on Stack Overflow Dec 21, 2014 by Fenil • edited Dec 5, 2017 by Uddhav Gautam
3

This fixed my problem:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    if(rootView==null){
        rootView = inflater.inflate(R.layout.MapFragment, container, false);
    }
    return rootView;
}

More here: https://colinyeoh.wordpress.com/2014/07/20/android-mapfragment-exception-when-clicked-twice/

answered on Stack Overflow Feb 23, 2016 by Spipau
0
MapFragment f = (MapFragment) getFragmentManager()
                                         .findFragmentById(R.id.mymap);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();

Actually you don't need to use this inside an onDestroyView. My case was to remove an inflated layout with a SupportMapFragment, and that was it.

answered on Stack Overflow Apr 8, 2019 by Caio Nilton • edited Apr 8, 2019 by Derek Pollard

User contributions licensed under CC BY-SA 3.0