MapFragment inside a fragment, app crash on reopening fragment

1

i have a mapfragment inside another fragment

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"
/>
</FrameLayout>

and im initializing map in oncreateview

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.event_fragment, null);
    try{
        events = new ArrayList<EventsModel>();

        try {               
            initilizeMap();

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

        double latitude = 24.875419;
        double longitude = 66.993293;
        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(latitude, longitude)).zoom(12).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        loadEvents().execute();
    }catch(Exception e){

    }

    return view;
}

its working fine but when i press back button and reopen this fragment it crashes

04-19 21:18:21.803: E/AndroidRuntime(31612): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f050006, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

then i googled it and put it in ondestroy

Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();

the above problem is resolved bu now it gives me nullpointer on reopening the fragment that had mapfragment in it may be because i removed it with above code but whats the solution now?

android
android-fragments
google-maps-android-api-2
android-nested-fragment
asked on Stack Overflow Apr 19, 2014 by Wasif Khalil • edited Apr 19, 2014 by Fllo

1 Answer

2

Please make sure that you have correctly checked the condition where map inflated is null or not while creating view for map fragment or while destroying.

put this one on onDestroy() method

SupportMapFragment mapFragment = ((SupportMapFragment) 
getActivity().getSupportFragmentManager().findFragmentById(R.id.map_location_sharing));

if(mapFragment != null) {
    FragmentManager fM = getFragmentManager();
    fM.beginTransaction().remove(mapFragment).commit();
answered on Stack Overflow Apr 27, 2014 by Justcurious • edited Jan 17, 2019 by Justcurious

User contributions licensed under CC BY-SA 3.0