Reputation: 438
I have a navigation drawer where Map is an option among other item. When I click in my Map it works. Than if I go to other navigation drawer item and back in Map again, it crash. Can anyone tell me the reason??
here is the code..
public class Map extends Fragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map, container, false);
setUpMapIfNeeded();
return view;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
@Override
public void onPause() {
super.onPause();
setUpMapIfNeeded();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
FragmentManager fm = getFragmentManager();
mMap = ((SupportMapFragment) fm.findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
LatLng latlang = new LatLng(53.0847558, 8.8208279);
mMap.addMarker(new MarkerOptions().position(latlang).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlang, 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}}
This is the xml
<fragment xmlns:android="schemas.android.com/apk/res/android";
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
and here is my error log message
01-24 16:00:16.335: E/AndroidRuntime(17656): FATAL EXCEPTION: main
01-24 16:00:16.335: E/AndroidRuntime(17656): android.view.InflateException: Binary
XML file line #3: Error inflating class fragment
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
de.apps28.com.Map.onCreateView(Map.java:27)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
01-24 16:00:16.335: E/AndroidRuntime(17656): Caused by:
java.lang.IllegalArgumentException: Binary XML file line #3: Duplicate id 0x7f050011,
tag null, or parent id 0x0 with another fragment for
com.google.android.gms.maps.SupportMapFragment
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
01-24 16:00:16.335: E/AndroidRuntime(17656): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-24 16:00:16.335: E/AndroidRuntime(17656): ... 18 more
Upvotes: 2
Views: 1972
Reputation: 1062
The problem is that the view gets destroyed once you navigate to another tab that is not a neighbor-tab of the one that includes the map. This results in the following line from your stacktrace:
Binary XML file line #3: Duplicate id 0x7f050011, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
So you are trying to recreate the map. A better solution is to save the view before it gets destroyed
@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;
}
Upvotes: 6