Figen Güngör
Figen Güngör

Reputation: 12569

Inner Map Fragment toruble when outer fragment called second time

I can access map when I call the fragment first time but when I call the fragment second time it gives me this caused by error:

12-26 09:36:41.306: E/AndroidRuntime(28156): Caused by: java.lang.IllegalArgumentException: Binary XML file line #164: Duplicate id 0x7f090012, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

I create the inner map fragment in my fragment class' onCreateView():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    View view = inflater.inflate(R.layout.howtogo_fragment, container,
            false);

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

here is the layout of my fragment:

<LinearLayout>

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

</LinearLayout>

Upvotes: 0

Views: 168

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

Try this .

private static GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        view = (RelativeLayout) inflater.inflate(R.layout.howtogo_fragment, container, false);

               MapLoad(); // For setting up the MapFragment

        return view;
    }

    /***** Sets up the map if it is possible to do so *****/
    public static void MapLoad() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) YOURACTIVITY.fragmentManager
                    .findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null)
                setUpMap();
        }
    }
private static void setUpMap() {
    // For showing a move to my loction button
    mMap.setMyLocationEnabled(true);
    // For dropping a marker at a point on the Map
  //  mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Home").snippet("Home Address"));
    // For zooming automatically to the Dropped PIN Location
  //  mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
        //    longitude), 12.0f));
}

Upvotes: 1

giolaoit
giolaoit

Reputation: 1

You can try my code: xml code:

<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"
android:background="@android:color/white"
tools:context="com.example.catalyst_home.GeolocationActivity$PlaceholderFragment" >

<RelativeLayout
    android:id="@+id/map_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

Fragment java:

private GoogleMap mMap;
private SupportMapFragment mMapFragment;
private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getChildFragmentManager()
                .beginTransaction();
        fragmentTransaction.add(R.id.map_root, mMapFragment);
        fragmentTransaction.commit();
    }
}

in onResum of fragment:

@Override
public void onResume() {
    super.onResume();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mMap = mMapFragment.getMap();
            if (mMap != null) {
                mMap.setMyLocationEnabled(true);
            }
        }
    }, 2000);
}

Upvotes: 0

Related Questions