【问题标题】:Replace the <fragment> tag with FragmentContainerView将 <fragment> 标签替换为 FragmentContainerView
【发布时间】:2021-01-18 01:48:03
【问题描述】:
【问题讨论】:
标签:
android
android-fragments
【解决方案2】:
感谢SABANTO 我用<androidx.fragment.app.FragmentContainerView> 替换了<fragment> 和<FrameLayout>(仅在活动中替换了<FrameLayout>)。它可以在 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.FragmentContainerView 和https://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 中)。