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?

Blo
  • 11,903
  • 5
  • 45
  • 99
Wasif Khalil
  • 2,217
  • 9
  • 33
  • 58
  • @WK try as per my answer and give me feedback on this. – M D Apr 19 '14 at 16:28
  • but here you need to implement `SupportMapFragment` instead of `MapFragment` it's bacause you have to implement `getSupportFragmentManager()` – M D Apr 19 '14 at 16:31
  • the above problem is solved but now when i press back from MapFragment it crash (it should take me to the 1st fragment i came from) 04-19 21:54:58.581: E/AndroidRuntime(975): java.lang.NullPointerException 04-19 21:54:58.581: E/AndroidRuntime(975): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:651) – Wasif Khalil Apr 19 '14 at 16:55
  • try to convert this MapFragment to SupportMapFragment – M D Apr 19 '14 at 17:00
  • not working class cast exception :-/ – Wasif Khalil Apr 19 '14 at 17:06
  • but you have to change this every where – M D Apr 19 '14 at 17:07
  • 1
    I'm not sure, but it might be possible that your exception is caused by the nested fragment. And you might be used the [`getChildFragmentManager()`](http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager%28%29) instead of `getFragmentManager()` method. – Blo Apr 19 '14 at 17:30
  • As @Fllo said. Use `getChildFragmentManager()` instead of `getFragmentManager()` and also check if you fragment is null before creating a new one. – LoveMeSomeFood Apr 19 '14 at 19:00

1 Answers1

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();
Justcurious
  • 2,201
  • 1
  • 21
  • 36