I'm trying to set up Google maps in my app inside a fragment with a marker. It works when I click on it the first time but when I click on another fragment then back to the Google maps one it crashes. Not sure what the issue is? Any help would be awesome, only beginner.
Heres code:
Xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
Java google maps fragment EDIT WORKING CODE BELOW
public class Fragment_8 extends Fragment{
static final LatLng BottleCapp = new LatLng(51.371986, 0.065593);
private GoogleMap map;
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.fragment_8, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker bottleapp = map.addMarker(new MarkerOptions().position(BottleCapp)
.title("BottleCapp"));
// Move the camera instantly to Bottlecapp with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(BottleCapp, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
getActivity().getSupportFragmentManager().popBackStack();
return view;
}
}
Error
06-15 21:53:46.673: E/AndroidRuntime(8677): FATAL EXCEPTION: main
06-15 21:53:46.673: E/AndroidRuntime(8677): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-15 21:53:46.673: E/AndroidRuntime(8677): at com.bottlecapp.bottlecapp.Fragment_8.onCreateView(Fragment_8.java:29)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.os.Handler.handleCallback(Handler.java:725)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.os.Looper.loop(Looper.java:137)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-15 21:53:46.673: E/AndroidRuntime(8677): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 21:53:46.673: E/AndroidRuntime(8677): at java.lang.reflect.Method.invoke(Method.java:511)
06-15 21:53:46.673: E/AndroidRuntime(8677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-15 21:53:46.673: E/AndroidRuntime(8677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-15 21:53:46.673: E/AndroidRuntime(8677): at dalvik.system.NativeStart.main(Native Method)
06-15 21:53:46.673: E/AndroidRuntime(8677): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f05003c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
06-15 21:53:46.673: E/AndroidRuntime(8677): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-15 21:53:46.673: E/AndroidRuntime(8677): ... 18 more