0

I have an xml layout which includes the following code:

<fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

This layout can be inflated in the main activity more than once and, in that case, should display multiple MapFragments. When that happens, however, I get the following error:

Duplicate id 0x7f0a0213, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

From my understanding, this happens because you can't have more than 1 <fragment> with the same id.

How can I inflate these views with these map fragments multiple times? Is it possible to assign different ID's to each fragment every time the layout is inflated?

EDIT: I found this. I guess I can't set the ID of the fragment itself programmatically.

George
  • 389
  • 5
  • 17

1 Answers1

0

just change the id from android:id="@+id/map" to android:id="@+id/map2" and etc. Please take into account, that every google map is expensive for performance and memory, so ask yourself do you really need more than one map

Jack
  • 1,545
  • 2
  • 11
  • 20
  • 1
    I think that the idea was to do this without copying the fragment over and over again. – Maarten Bodewes Dec 11 '19 at 15:28
  • I'd like to be able to do that, but I don't know how or if it is possible. As I said, the map is added by inflating a layout xml (which includes the map in a ) and this can occur up to 3 times. This is a contact app, which can display up to 3 addresses, and I want to have a map image below each address to display the location of the address. I could, perhaps, take a snapshot of the address location at each iteration, and add that instead, if possible. – George Dec 11 '19 at 15:33
  • look into Listview https://developer.android.com/reference/android/widget/ListView. It might be that eventually, you will need more than 3 maps. And if its something simple its better to go with ListView and not recyclerView https://developer.android.com/guide/topics/ui/layout/recyclerview – Jack Dec 17 '19 at 12:35