【问题标题】:FATAL EXCEPTION: main Process: Binary XML file line #81: Binary XML file line #81: Error inflating class fragment致命异常:主进程:二进制 XML 文件第 81 行:二进制 XML 文件第 81 行:膨胀类片段时出错
【发布时间】:2017-12-28 06:01:29
【问题描述】:

当我打开对话框时它工作正常并且当我第二次单击它崩溃时,我陷入了这个问题。 请提供一些解决此问题的建议。 谢谢你。

这是我的 xml -

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

                    android:background="@color/white" />

这是我的 java 代码 -

                    try {
                        FragmentManager myFragmentManager = getActivity().getSupportFragmentManager();

                        SupportMapFragment mySupportMapFragment
                                = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map_container);


                        mySupportMapFragment.getMapAsync(
                                new OnMapReadyCallback() {
                                    @Override
                                    public void onMapReady(GoogleMap mGoogleMap) {
                                        googleMap = mGoogleMap;
                                        AppDelegate.LogT("Google Map ==" + googleMap);
                                        showMap();
                                    }
                                }
                        );
                    } catch (Exception e) {
                        AppDelegate.LogE(e);
                    }

【问题讨论】:

  • 你能添加你的布局代码吗?
  • 添加你的 Xml 代码
  • 请分享您的 xml 代码整个标签,它位于这 81 行之间,以便我们轻松理解
  • 已添加 XML 代码。
  • 添加你的全班代码

标签: android google-maps android-studio


【解决方案1】:

或者你可以这样做:

定义这个

   SupportMapFragment mySupportMapFragment;

稍后在课堂上给它充气:

//如果它为null,则仅inflate

  if(mSupportMapFragment==null)
   {
      mySupportMapFragmen = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map_container);
   }

【讨论】:

    【解决方案2】:

    当我打开对话框时它工作正常,当我第二次单击时 它崩溃了

    因为一旦对话框打开,片段就会膨胀。当你第二次打开它时,它会第二次充气,它已经充气了。试试下面的代码。在此它会删除对话框关闭时的片段。

    在这里,在删除中添加您的片段对象。

    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(final DialogInterface arg0) {
                    // do something
                    getFragmentManager().beginTransaction().remove(SupportMapFragment).commit();
    
    
                }
            });
    

    【讨论】:

    • 确保在对话框和片段初始化后使用此代码。
    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2020-10-25
    • 1970-01-01
    • 2014-05-04
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多