Duplicate Id, Tag Null, or Parent Id With Another Fragment For Com.Google.Android.Gms.Maps.Mapfragment

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

The answer Matt suggests works, but it cause the map to be recreated and redrawn, which isn't always desirable.
After lots of trial and error, I found a solution that works 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;
}

For good measure, here's "map.xml" (R.layout.map) with R.id.mapFragment (android:id="@+id/mapFragment"):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

I hope this helps, but I can't guarantee that it doesn't have any adverse effects.

Edit: There were some adverse effects, such as when exiting the application and starting it again. Since the application isn't necessarily completely shut down (but just put to sleep in the background), the previous code i submitted would fail upon restarting the application. I've updated the code to something that works for me, both going in & out of the map and exiting and restarting the application, I'm not too happy with the try-catch bit, but it seem to work well enough. When looking at the stack trace it occurred to me that I could just check if the map fragment is in the FragmentManager, no need for the try-catch block, code updated.

More edits: Turns out you need that try-catch after all. Just checking for the map fragment turned out not to work so well after all. Blergh.

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

Make sure your Activity extends AppCompatActivity which in appcompat-v7 support library.

Also make sure using android.support.v4.app.Fragment instead android.app.Fragment

At last, confirm that getSupportFragmentManager() instead of getFragmentManager()

Error inflating class fragment: Duplicate id , tag null, or parent id with another fragment

I solved it now!

The another solution is linked answer worked for me well.

https://stackoverflow.com/a/14484640/3960528

As explained there, I added onDestroyView() in `fragment that contains maps.

@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mymap);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}

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

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>

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

Not sure if this is the right process but it works

@Override
protected void onStop() {
mGoogleApiClient.disconnect();
FragmentManager fm = fragmentManager;
Fragment fragment = (fm.findFragmentById(R.id.mapDialog));

if (fragment.isResumed()) {
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
super.onStop();
}


Related Topics



Leave a reply



Submit