【问题标题】:Replace the <fragment> tag with FragmentContainerView将 <fragment> 标签替换为 FragmentContainerView
【发布时间】:2021-01-18 01:48:03
【问题描述】:

Android Studio 提供替换 Google Maps 片段中的 &lt;fragment&gt; 标记。在我没有Navigation 的应用程序中,但可能会添加。我应该用&lt;FragmentContainerView&gt; 替换&lt;fragment&gt; 标签吗?

查看https://proandroiddev.com/android-fragments-fragmentcontainerview-292f393f9ccf发现我们也可以用&lt;androidx.fragment.app.FragmentContainerView&gt;替换&lt;FrameLayout&gt;。我们应该这样做吗?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    感谢SABANTO 我用&lt;androidx.fragment.app.FragmentContainerView&gt; 替换了&lt;fragment&gt;&lt;FrameLayout&gt;(仅在活动中替换了&lt;FrameLayout&gt;)。它可以在 debug 构建中正常工作,但不能在崩溃的 release 中工作。

    android.view.InflateException: Binary XML file line #16 in com.example:layout/fragment_show_map: Binary XML file line #16 in com.example:layout/fragment_show_map: Error inflating class androidx.fragment.app.FragmentContainerView
    Caused by: android.view.InflateException: Binary XML file line #16 in com.example:layout/fragment_show_map: Error inflating class androidx.fragment.app.FragmentContainerView
    Caused by: androidx.fragment.app.FragmentActivity$HostCallbacks: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists
    Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
    

    查看ClassNotFoundException when using androidx.fragment.app.FragmentContainerViewhttps://stackoverflow.com/a/61365688/2914140 我发现我们应该在proguard-rules.pro 中添加一两行,具体取决于您在XML 文件中的内容:

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        ...
    

    proguard-rules.pro:

    #-------------------------------------------------
    # JetPack Navigation
    # This fixes: 
    # Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists
    # Caused by: androidx.fragment.app.FragmentActivity$HostCallbacks: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists
    #-------------------------------------------------
    -keepnames class androidx.navigation.fragment.NavHostFragment
    -keepnames class com.google.android.gms.maps.SupportMapFragment
    

    不要忘记在 release build 中测试您的应用程序(当然还有在 debug build 中)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2021-07-30
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多