Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapFragment in Fragment, alternatives?

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; } 
like image 648
Kyu_ Avatar asked Mar 15 '13 13:03

Kyu_


People also ask

What is MapFragment?

public class MapFragment extends Fragment. A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.

What is mapview?

A View which displays a map (with data obtained from the Google Maps service). When focused, it will capture keypresses and touch gestures to move the map.

What is getMapAsync?

getMap() is deprecated public void getMapAsync (OnMapReadyCallback callback) Sets a callback object which will be triggered when the GoogleMap instance is ready to be used. Note that: This method must be called from the main thread. The callback will be executed in the main thread.


1 Answers

Use SupportMapFragment to overcome this error:

In fragment layout

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

In Your Fragment onCreateView

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);          if (mapFragment != null) {             mapFragment.getMapAsync(this);         } 

Since your layout is in the Fragment's layout, therefore the SupportMapFragment is the child layout of your fragment. Hence use getChildFragmentManager() which is Fragment's FragmentManager

like image 53
Adnan Avatar answered Sep 22 '22 08:09

Adnan