我想尝试新的导航库.在跟随this guideline后,我在运行时遇到错误:
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中.这个文件很简单:
我查看了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之外的库,因为它可能尚未迁移. Google上我唯一能找到androidx.navigation的地方是here.不幸的是Gradle无法下载它.
编辑
我的gradle.properties中也启用了jetifier工具.
android.enableJetifier=true
android.useAndroidX=true
更新
它在this answer中提到的Android Studio 3.2 Canary 17中得到修复.不要忘记使缓存无效并重新启动以删除代码中的警告.
最佳答案