我正在尝试在我的应用程序中设置带有标记的片段内的谷歌地图。当我第一次点击它时它可以工作,但是当我点击另一个片段然后回到谷歌地图时它崩溃了。不确定是什么问题?任何帮助都会很棒,只有初学者。
继承人代码:
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 谷歌地图片段编辑下面的工作代码
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;
}
}
错误
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