我想尝试新的导航库。遵循此指南后,我在运行时遇到错误:
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists, is public, and has an empty constructor that is public
在资源文件中activity_home.xml
。这个文件很简单:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeActivity">
<fragment
android:id="@+id/fragment_navigation_host"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_home" />
</FrameLayout>
我查看了的源代码,NavHostFragment
我注意到它android.support.v4.app.Fragment
在我的整个应用程序使用时使用androidx.fragment.app.Fragment
。
我不相信这是问题所在,但我在下面包含了我的一些依赖项:
// AndroidX
implementation "androidx.appcompat:appcompat:$appCompatVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
implementation "androidx.room:room-runtime:$roomVersion"
implementation "androidx.room:room-rxjava2:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$navigationVersion"
implementation "android.arch.navigation:navigation-ui-ktx:$navigationVersion"
如您所见,我正在使用 AndroidX 中的库,但 Navigation 除外,因为它可能尚未迁移。谷歌上我能找到的唯一地方androidx.navigation
就是这里。不幸的是,Gradle 无法下载它。
编辑
我的gradle.properties
.
android.enableJetifier=true
android.useAndroidX=true
更新
如本答案所述,它已在 Android Studio 3.2 Canary 17 中修复。不要忘记使缓存无效并重新启动以删除代码中的警告。