同时显示一个活动中的多个地图片段

问题描述 投票:0回答:1

我有一个活动,其中包含多个地址(街道,城市,国家/地区和邮政编码),并将其显示给用户。在每个地址下,我想显示一个MapFragment,传递地址信息并在地图上向用户显示该点。

[尝试时,应用程序崩溃了,出现以下错误。它指出了我要为包含所有地址实例的父级布局充气的位置。

Caused by: java.lang.IllegalArgumentException: Binary XML file line #45: Duplicate id 0x7f0a0211, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment

是否有解决此错误和/或否则显示多个MapFragments的方法?

Edit:我希望我的布局看起来像这样。我可以显示多个文本条目,但是一旦尝试添加2个mapFragments,就会崩溃。

XML代码:

<fragment
        android:id="@+id/map1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@id/contact_address_info"
        android:layout_toEndOf="@id/contact_address_image"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:fitsSystemWindows="true"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <fragment
        android:id="@+id/map2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@id/contact_address_info"
        android:layout_toEndOf="@id/contact_address_image"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:fitsSystemWindows="true"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <fragment
        android:id="@+id/map3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@id/contact_address_info"
        android:layout_toEndOf="@id/contact_address_image"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:fitsSystemWindows="true"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

Java代码:

SupportMapFragment mapFragment1;
        mapFragment1 = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
        if (mapFragment1 != null) mapFragment1.getMapAsync(this);

                SupportMapFragment mapFragment2;
                mapFragment2 = (SupportMapFragment)
         getSupportFragmentManager().findFragmentById(R.id.map2);
                if (mapFragment2 != null) mapFragment2.getMapAsync(this);

                SupportMapFragment mapFragment3;
                mapFragment3 = (SupportMapFragment)
         getSupportFragmentManager().findFragmentById(R.id.map3);
                if (mapFragment3 != null) mapFragment3.getMapAsync(this);

enter image description here

java android supportmapfragment
1个回答
0
投票

每次只需为新的MapFragment充气:

MapFragment mapFragment1 = MapFragment.newInstance();
        mapFragment.getMapAsync(this);
        getFragmentManager()
                .beginTransaction()
                .add(R.id.dynamic_layout_holder, mapFragment)
                .commit();
MapFragment mapFragment2 = MapFragment.newInstance();
        mapFragment2.getMapAsync(this);
        getFragmentManager()
                .beginTransaction()
                .add(R.id.dynamic_layout_holder, mapFragment2)
                .commit();
© www.soinside.com 2019 - 2024. All rights reserved.