Duplicate id, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

12,808

Solution 1

Its old but i solved the problem like this.

Fragment.java

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class Fragment12 extends SherlockFragment {
    //create hashmap
            private HashMap<Marker, Integer> hash = new HashMap<Marker, Integer>();
            private HashMap<Marker, Class<?>> hashclass = new HashMap<Marker, Class<?>>();
           //

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment12, null);
                return rootView;
            }

    @Override
    public void onViewCreated(View v, Bundle savedInstanceState){
        super.onViewCreated(v, savedInstanceState);

    final LatLng Initial = new LatLng(-34.673009, -58.474111);
    final LatLng FADU = new LatLng(-34.542163, -58.443716);
    final LatLng UNO = new LatLng(-34.524924, -58.576421);
    final LatLng DOS = new LatLng(-34.603489, -58.439344);
    final LatLng TRES = new LatLng(-34.646890, -58.513357);
    final LatLng CUATRO = new LatLng(-34.512189, -58.489223);
    final LatLng CINCO = new LatLng(-34.617692, -58.376514);
    final LatLng SEIS = new LatLng(-34.744174, -58.252917);
    final LatLng SIETE = new LatLng(-34.578326, -58.439691);
    final LatLng OCHO = new LatLng(-34.606389, -58.458911);
    final LatLng NUEVE = new LatLng(-34.756687, -58.402807);
    final LatLng DIEZ = new LatLng(-34.621184, -58.483144);


GoogleMap googlemap;

        googlemap  = ((SupportMapFragment)  getFragmentManager().findFragmentById(R.id.map12)).getMap();

        googlemap.setMyLocationEnabled(true);
        googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(Initial, 10);
        googlemap.animateCamera(update);


        //modify
        Marker marker1 = googlemap.addMarker(new MarkerOptions().position(FADU).title("FADU").snippet("Facultad de Arquitectura, Diseño y Urbanismo").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1)));
        hash.put(marker1, R.drawable.logo);
        hashclass.put(marker1, Contacto.class);


            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2)));
    hash.put(marker11, R.drawable.renderak);
    hashclass.put(marker11, RenderAK.class);

    googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener(){

        @Override
        public void onInfoWindowClick(Marker marker) {
            // TODO Auto-generated method stub
            Class<?> cls = hashclass.get(marker);
            Intent i = new Intent(getActivity(), cls);
            startActivity(i);
        }
    });

    googlemap.setInfoWindowAdapter(new InfoWindowAdapter() {

     @Override
     public View getInfoWindow(Marker marker) {

     View v = getLayoutInflater(null).inflate(R.layout.infowindow, null);

     TextView titulo = (TextView) v.findViewById(R.id.titulo);
     TextView direccion = (TextView) v.findViewById(R.id.direccion);
     ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));

     titulo.setText(marker.getTitle());
     direccion.setText(marker.getSnippet());

     if(hash.get(marker) != null)
        imagen.setImageDrawable(getResources().getDrawable(hash.get(marker)));

     return v;

    }
        //..
     @Override
        public View getInfoContents(Marker marker) {
            // TODO Auto-generated method stub
            return null;
        }
     });
    }
@Override
public void onPause() {
    super.onPause();

 }
@Override
public void onDestroyView() {

    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map12));  
    if (fragment != null){
        getActivity().getSupportFragmentManager().beginTransaction()
        .remove(fragment)
        .commit();          
        }      
    }
}

fragment.xml

<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" >

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

</RelativeLayout>

Solution 2

Use the following code. It is working for me

private static View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
    ViewGroup parent = (ViewGroup) view.getParent();
    if (parent != null)
        parent.removeView(view);
}
try {
    view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
    /* map is already there, just return view as it is */
}
return view;
}
Share:
12,808
TheX_H
Author by

TheX_H

Updated on June 04, 2022

Comments

  • TheX_H
    TheX_H almost 2 years

    Basically the app crash when I open one fragment after a period of inactivity, or Having left it in the background, or after unlocking the phone with the app open.

    I read this answer, but honestly I could not solve it, if anyone could correct me specifically my code and explain it, I'll be grateful

    LogCat:

    07-06 15:32:25.992: E/AndroidRuntime(4155): FATAL EXCEPTION: main
    07-06 15:32:25.992: E/AndroidRuntime(4155): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at com.TheX.fadubusca.Fragment4.onCreateView(Fragment4.java:37)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.os.Handler.handleCallback(Handler.java:587)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.os.Looper.loop(Looper.java:130)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.app.ActivityThread.main(ActivityThread.java:3687)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at java.lang.reflect.Method.invoke(Method.java:507)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at dalvik.system.NativeStart.main(Native Method)
    07-06 15:32:25.992: E/AndroidRuntime(4155): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f050054, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
    07-06 15:32:25.992: E/AndroidRuntime(4155):     ... 19 more
    

    I have multiple mapfragment, are put one, but the problem is with any of the 21 who i have

    Fragment.java:

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import com.actionbarsherlock.app.SherlockFragment;
    import com.google.android.gms.maps.CameraUpdate;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    
    public class Fragment0 extends SherlockFragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment0, container, false);
    return rootView;
    }
    
    @Override
    public void onViewCreated(View v, Bundle savedInstanceState){
    super.onViewCreated(v, savedInstanceState);
    
    final LatLng Initial = new LatLng(-34.673009, -58.474111);
    final LatLng FADU = new LatLng(-34.542163, -58.443716);
    
    
    
    GoogleMap googlemap;
    
    googlemap  = ((SupportMapFragment)   getFragmentManager().findFragmentById(R.id.map0)).getMap();
    
    googlemap.setMyLocationEnabled(true);
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(Initial, 10);
    googlemap.animateCamera(update);
    
    
    googlemap.addMarker(new MarkerOptions().position(FADU).title("FADU").snippet("Facultad de Arquitectura, Diseño y Urbanismo")
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    
    }
    
    @Override
    public void onPause() {
    super.onPause();
    
    }
    @Override
    public void onDestroyView() {
    
    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map0));  
    if (fragment != null){
    getActivity().getSupportFragmentManager().beginTransaction()
    .remove(fragment)
    .commit();          
    }
    }
    

    fragment.xml:

    <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" >
    
    <fragment
    android:id="@+id/map4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
    
    </RelativeLayout>
    
  • theknut
    theknut over 9 years
    Nice steal Mr. Shafi. Next time make a reference if you copy code from other answers.
  • surhidamatya
    surhidamatya about 9 years
    Original answer is over here stackoverflow.com/questions/14083950/…
  • Sanjay Kumar
    Sanjay Kumar almost 7 years
    You can't perform this action after onsavedinstance
  • Jitesh Upadhyay
    Jitesh Upadhyay over 6 years