二进制 XML 文件第 1 行:膨胀 class 片段 MapFragment 时出错

Binary XML file line #1: Error inflating class fragment MapFragment

我有一个带有一些片段的导航抽屉。每个 Fragment 在布局中都有一个 MapFragment。最初,当应用程序运行导航抽屉时,点击一次打开片段,但是当我第二次点击同一个导航项打开同一个片段时(在访问其他片段之后)我在 onCreateView() 中得到以下错误

Binary XML file line #1: Error inflating class fragment 

下面是我的片段代码:

user_map_Fragment.xml

 <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:id="@+id/parentContainer"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:name="com.google.android.gms.maps.MapFragment"/>

......
</LinearLayout>

MainFragment.cs

public class MainFragement : Android.Support.V4.App.Fragment, IOnMapReadyCallback, ILocationListener, Android.Views.View.IOnKeyListener
    {

        private GoogleMap mMap;

        private LocationManager _locationManager;
        private string _locationProvider;
        private Android.Locations.Location _currentLocation;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (v == null)
            {
                v = inflater.Inflate(Resource.Layout.Main, container, false);

                SetUpMap();
                InitializeLocationManager();

            }
            return v;
        }
}



private void SetUpMap()
        {
            if (mMap == null)
            {
                Activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
            }
        }

崩溃日志:

06-22 10:56:50.798 E/mono-rt (15470): java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f0a0099, tag null, or parent id 0x7f0a007b with another fragment for com.google.android.gms.maps.MapFragment
06-22 10:56:50.798 E/mono-rt (15470):   at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2136)
06-22 10:56:50.798 E/mono-rt (15470):   at android.app.Activity.onCreateView(Activity.java:5516)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
06-22 10:56:50.798 E/mono-rt (15470):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
06-22 10:56:50.798 E/mono-rt (15470):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
06-22 10:56:50.798 E/mono-rt (15470):   at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
06-22 10:56:50.798 E/mono-rt (15470):   at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
06-22 10:56:50.798 E/mono-rt (15470):   at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
06-22 10:56:50.798 E/mono-rt (15470):   at md5ec4b17f0066095dde498afe2bdaa8cb9.PeopleCheckinFragment.n_onCreateView(Native Method)
06-22 10:56:50.798 E/mono-rt (15470):   at md5ec4b17f0066095dde498afe2bdaa8cb9.PeopleCheckinFragment.onCreateView(PeopleCheckinFragment.java:39)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
06-22 10:56:50.798 E/mono-rt (15470):   at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:493)
06-22 10:56:50.798 E/mono-rt (15470):   at android.os.Handler.handleCallback(Handler.java:739)
06-22 10:56:50.798 E/mono-rt (15470):   at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 10:56:50.798 E/mono-rt (15470):   at android.os.Looper.loop(Looper.java:135)
06-22 10:56:50.798 E/mono-rt (15470):   at android.app.ActivityThread.main(ActivityThread.java:5930)
06-22 10:56:50.798 E/mono-rt (15470):   at java.lang.reflect.Method.invoke(Native Method)
06-22 10:56:50.798 E/mono-rt (15470):   at java.lang.reflect.Method.invoke(Method.java:372)
06-22 10:56:50.798 E/mono-rt (15470):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
06-22 10:56:50.798 E/mono-rt (15470):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

我该如何解决这个问题?

而不是这个

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

使用这个

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

在javaclass

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class ClinicFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;

    public static ClinicFragment newInstance() {
        ClinicFragment fragment = new ClinicFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return view;
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

尝试改变

android:name="com.google.android.gms.maps.SupportMapFragment"
//and
Activity.FragmentManager.FindFragmentById<SupportMapFragment>(Resource.Id.map).GetMapAsync(this);

而不是

android:name="com.google.android.gms.maps.MapFragment"
//and
Activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);

编辑 1

删除您的 android.app.Fragment 并导入支持包

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

编辑 2

试试这个

SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
    .findFragmentById(R.id.map));

这可能对你有帮助

android:hardwareAccelerated="true"AndroidManifest.xml

我的 MapsActivity

对我有用。

就我而言,即使我使用的是 SupportMapFragment,我也忘记在 google 地图中添加 API_KEY。

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

清单中:

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />