I need your help... I work on it until 3 days. My app is working with fragments. One of these fragments has to display a map from the Google Maps V2 api for Android.
Currently, I'm using a MapFragment, but no surprise, a fragment in a fragment is not a good idea, but it works, the map is displaying, i can edit it but when I switch of main fragment and return on it.
Caused by: java.lang.IllegalArgumentException: Binary XML file line #59: Duplicate id 0x7f070041, tag null, or parent id 0x7f070040 with another fragment for com.google.android.gms.maps.MapFragment
at android.app.Activity.onCreateView(Activity.java:4252)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
This is the cause when I go on another fragment and return to the one which contains the map. I'm searching until 3 days to fix this but no great results.
To resume for you, I've an Activity which calls a fragment which contains a MapFragment in the layout file. If you need more, just ask :)
Thanks
Edit : Here is the code to change Fragment in the main Activity
private void swtichFragment(Fragment fragment, Bundle bundle)
{
fragment.setBundle(this, bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.rightFragmentPlaceHolder, fragment);
fragmentTransaction.commit();
mRightFragment = fragment;
}
Use SupportMapFragment to overcome this error:
In fragment layout
In Your Fragment onCreateView
Since your layout is in the
Fragment's
layout, therefore theSupportMapFragment
is the child layout of your fragment. Hence usegetChildFragmentManager()
which isFragment's FragmentManager
As described here
To display MapFragment inside the fragment(NestedFragment): At this point I believe you have
where.xml
class:
Documentation:
source:http://developer.android.com/about/versions/android-4.2.html#NestedFragments
This will also fix for:
In your layout file
In your fragment
onCreate()
, reference the map fragment in your fragment layout file usingchildFragmentManager
After lot of mistakes I finally made it , here is my MapView Fragment Class :-
My Xml file looks like this:-
And result looks like this :-
Most Important thing to note is that DO Not Mix app.Fragment with v4.Fragments else app will crash badly.
As you can see I have used app.Fragment to attach and remove my MapView Fragment
Hope it will help Somebody
Use MapView instead of MapFragment in your Fragment's layout. Remember to call MapView's lifecycle methods:
as described here.
Btw. you shouldn't be using MapFragment, only SupportMapFragment and support library.
Edit:
If you switch to support library, you can use code from comment #1 here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1
In Your Class
mylayout.xml