我正在尝试在片段中的 viewpager (fragmentpager) 中使用谷歌地图 v2。如果我滑动到第四个片段(查看寻呼机),它可以正常工作,但是如果我回到第一个片段然后回到第四个片段 - 使用地图 - 我的应用程序就会死掉。
07-04 19:38:20.937: E/AndroidRuntime(20175): FATAL EXCEPTION: main
07-04 19:38:20.937: E/AndroidRuntime(20175): android.view.InflateException: Binary XML
file line #13: Error inflating class fragment
...
07-04 19:38:20.937: E/AndroidRuntime(20175): Caused by:
java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f050010,
tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
07-04 19:38:20.937: E/AndroidRuntime(20175): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-04 19:38:20.937: E/AndroidRuntime(20175): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
安卓Java:
public class LocationDetail extends Fragment {
private SupportMapFragment mMapFragment;
private GoogleMap map;
private EventAdapter listAdapter ;
protected MainActivity activity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.map, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
}
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_container)).getMap();
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions()
.position(new LatLng(48.397141, 9.98787)).title(" Theatro Club Ulm"));
map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(48.397141, 9.98787) , 14.0f) );
return vw;
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<fragment
android:id="@+id/map_container"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.dr.diskoapp.MainActivity"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list">
</ListView>
</LinearLayout>
</LinearLayout>
我已经看到有很多线程有类似的问题,但没有解决方案:-/
这次也许有人可以帮忙。
---------- 编辑 ----- 我找到了解决方案:-)
public void onDestroyView() {
super.onDestroyView();
try {
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_container));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}