1

I'm trying to add a google mapview to an existing fragment. Following the instructions from the developer docs, I've included the following xml in my fragment:

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

However, I end up getting an IllegalArgumentException every time:

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): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285) 02-28 18:54:21.133: E/AndroidRuntime(11300): at android.view.LayoutInflater.createViewFromTag(Layout

Any workarounds for this?

munkay
  • 1,883
  • 2
  • 16
  • 15
  • Actually your are trying to add MapFragment (from your code) and not a Map View. and i don't think it is possible because fragments are designed to sit inside of activities and not other fragments. – Emil Adz Mar 01 '13 at 00:09
  • Is it possible just to add a MapView, without using the MapFragment? Ideally I would just like to include a MapView as part of my layout, like any other widget. Thanks! – munkay Mar 01 '13 at 00:10
  • For what i know MapView is GoogleMaps API V1 object and can not be used in V2. what exactly are you trying to achieve? – Emil Adz Mar 01 '13 at 00:12
  • I'm trying to embed a map in a fragment. My fragment looks like: [map] [title] [body text] – munkay Mar 01 '13 at 00:39
  • 1
    Try to put a LinearLayout inside of your parent fragment and maybe to put the map fragment inside along with other widgets you want. in addition check this link: http://stackoverflow.com/questions/13812988/android-maps-v2-mapview-inside-custom-fragment-npe – Emil Adz Mar 01 '13 at 00:47
  • You seem to be mixing and matching `MapFragment` and `SupportMapFragment`, based on your layout and your error. – CommonsWare Mar 01 '13 at 02:50
  • @CommonsWare thanks, good catch. i've tried both supportmapfragment and mapfragment, same result. – munkay Mar 01 '13 at 05:24
  • @emil-adj, your link worked, I was able to embed a mapview sucessfully. thanks! – munkay Mar 01 '13 at 05:24

1 Answers1

0

You cannot inflate a layout into a fragment when that layout includes a fragment http://developer.android.com/about/versions/android-4.2.html#NestedFragments

MapFragment should be added dynamically in code, for example:

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

and in xml layout:

<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>
Vladimir
  • 514
  • 3
  • 6