使用 androidx.fragment.app.FragmentContainerView 时出现 ClassNotFoundException

Ran*_*ndy 14 android android-fragments

我正在尝试从使用 a 过渡FrameLayout到 usingFragmentContainerView并且从我看过的教程来看,这应该很容易。但是,当我在进行更改后运行我的应用程序时,我得到

Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.ClassNotFoundException: androidx.fragment.app.FragmentContainerView
Run Code Online (Sandbox Code Playgroud)

我的布局文件非常简单:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/app_bar_main">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我需要在我的 build.gradle 中添加一些东西吗?为什么找不到类?

Com*_*are 31

这是androidx.fragment1.2.0 或更高版本:

implementation "androidx.fragment:fragment:1.2.0"
Run Code Online (Sandbox Code Playgroud)


Coo*_*ind 7

感谢@Salam El-Banna和他的链接,我发现如果应用程序在release构建中崩溃,我们应该添加proguard-rules.pro(一两行,取决于android:namein <FragmentContainerView>

#-------------------------------------------------
# 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.Fragment$InstantiationException: 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
Run Code Online (Sandbox Code Playgroud)