android.view.InflateException:二进制 XML 文件第 14 行:膨胀 class 片段时出错

android.view.InflateException: Binary XML file line #14: Error inflating class fragment

当我单击后退按钮并返回到位置片段时遇到了一些问题,出现以下错误:

android.view.InflateException: Binary XML file line #14: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view.LayoutInflater.inflate(LayoutInflater.java:504) at android.view.LayoutInflater.inflate(LayoutInflater.java:414) at com.everstudio.nadejenew.LocationFragment.onCreateView(LocationFragment.java:73) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:807) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2360) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:823) at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:776) at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:178) at com.everstudio.nadejenew.MainActivity.onOptionsItemSelected(MainActivity.java:110) at android.app.Activity.onMenuItemSelected(Activity.java:2885) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408) at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113) at android.support.v7.widget.ToolbarWidgetWrapper.onClick(ToolbarWidgetWrapper.java:187) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.IllegalArgumentException: Binary XML file line #14: Duplicate id 0x7f0f00c6, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3447) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:177) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  at com.everstudio.nadejenew.LocationFragment.onCreateView(LocationFragment.java:73)  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)  at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:807)  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2360)  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)  at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:823)  at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:776)  at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:178)  at com.everstudio.nadejenew.MainActivity.onOptionsItemSelected(MainActivity.java:110)  at android.app.Activity.onMenuItemSelected(Activity.java:2885)  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)  at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)  at android.support.v7.widget.ToolbarWidgetWrapper.onClick(ToolbarWidgetWrapper.java:187)  at android.view.View.performClick(View.java:4780)  at android.view.View$PerformClick.run(View.java:19866)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我确实检查了很多 post 并尝试了所有解决方案,但它不起作用。好像其他菜单项都没有问题,只有这个位置页面报错。

fragment_location.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/white"
    android:spinnerMode="dropdown"/>

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

</RelativeLayout>

LocationFragment.java

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_location, container, false);// this is the line 73
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    return view;
}

我认为主要原因是您使用名称而不是 class、

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

尝试更新此代码库。

  FragmentManager fmanager = getSupportFragmentManager();
   Fragment fragment = fmanager.findFragmentById(R.id.map);
        SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
        GoogleMap supportMap = supportmapfragment.getMap()

您正在使用 childSupportmanager ,还有这一行 view = inflater.inflate(R.layout.fragment_location, container, false); 您从未从中锚定您的地图视图,例如 view.findViewVyId 您从未这样做过

尝试在 LocationFragment.java 中删除 onDestroyView 上的 mapFragment。

@Override
public void onDestroyView() {
    super.onDestroyView();
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    if (mapFragment != null)
        getChildFragmentManager().beginTransaction().remove(mapFragment).commitAllowingStateLoss();
}