0

I am trying to show fragments in Main Activity using navigation Drawer and one of the fragments includes Google Map. When I invoke the Fragment first time, the map shows up fine. But second time it throws exception as follows - Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f05000c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

here is my code for layout and classes. activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"       
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

fragment_home.xml -

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

MainActivity.java

public class MainActivity extends FragmentActivity {
.....
.........
//Calling below code on a Listner on Navigation Drawer
fragment = new HomeFragment();
if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

HomeFragment.java

public class HomeFragment extends Fragment {

  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;
public HomeFragment(){}

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

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

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
            Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.ic_launcher)));

            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

    return rootView;
}

public void onDestroy() {        
       // clearReferences();
        super.onDestroy();

        FragmentManager fm = getFragmentManager();

        Fragment xmlFragment = fm.findFragmentById(R.id.map);
        if (xmlFragment != null) {
            fm.beginTransaction().remove(xmlFragment).commit();
        }
}   

}

I am even removing the fragment in OnDestroy but when the second time this fragment is called , i get duplicate ID exception. If I call another fragment and then call this map fragment then it works fine. basically calling another fragment destroys it but calling same map fragment consecutively does not. What Am I missing / doing wrong here ?

Amit
  • 1,642
  • 2
  • 13
  • 22
  • You should remove `Fragment` in `onDestroyView()`. take a look at [http://stackoverflow.com/questions/24181356/android-fragment-application-crashed-after-closing-application/24181441#24181441](http://stackoverflow.com/questions/24181356/android-fragment-application-crashed-after-closing-application/24181441#24181441) – M D Mar 29 '15 at 12:19
  • @ M D - tried onDestryView() but same thing. BTW, i did had the same cleanup done in onDestroy() so not sure if it should make a difference. – Amit Mar 29 '15 at 12:43

0 Answers0