将Google Maps Android v2添加到Fragment时出现IllegalArgumentException

时间:2013-03-01 00:04:46

标签: android android-maps-v2

我正在尝试将google mapview添加到现有片段中。 按照开发人员文档的说明,我在我的片段中包含了以下xml:

<fragment
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment" />

但是,每次我最终都会收到IllegalArgumentException:

02-28 18:54:21.133: E/AndroidRuntime(11300): Caused by:     java.lang.IllegalArgumentException: Binary XML file line #158: Duplicate id 0x7f050019, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

02-28 18:54:21.133:E / AndroidRuntime(11300):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 02-28 18:54:21.133:E / AndroidRuntime(11300):在android.view.LayoutInflater.createViewFromTag(布局

对此有任何解决方法吗?

1 个答案:

答案 0 :(得分:0)

当布局包含片段时,您无法将布局膨胀为片段 http://developer.android.com/about/versions/android-4.2.html#NestedFragments

MapFragment应该在代码中动态添加,例如:

MapFragment fragment = new MapFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment).commit();

并在xml布局中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

</RelativeLayout>