NavHostFragment not accessible from XML

一世执手 提交于 2019-12-09 16:45:12

问题


I wanted to try new Navigation library. After following this guideline I experienced error at runtime:

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

in resource file activity_home.xml. This file is very simple:

<?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>

I looked at the source code of NavHostFragment and I noticed that it uses android.support.v4.app.Fragment while whole my application uses androidx.fragment.app.Fragment.

I'm not convinced that this is the issue, but I'm including some of my dependencies below:

// 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"

As you can see I'm using libraries from AndroidX except Navigation, because probably it's not migrated yet. The only place on Google where I can find androidx.navigation is here. Unfortunately Gradle is unable to download it.

Edit

I also have jetifier tool enabled inside my gradle.properties.

android.enableJetifier=true
android.useAndroidX=true

Update

It is fixed in Android Studio 3.2 Canary 17 as mentioned in this answer. Don't forget to invalidate cache and restart in order to remove warnings in code.


回答1:


Yep, as mentioned by Levi Albuquerque, this is a known bug in latest Canary Android Studio release (14). You can vote to this bug, subscribe and provide some useful information here.

Update:

Issue will be fixed in Android Gradle plugin 3.2.0-alpha17




回答2:


Apparently, see here and here, the use of the Jetifier and Android X is still under refactor, In this google i/o talk they've asked us to wait for Canary 15 which has some bug fixes.

Try to use the navigation library with the old support library for now.

Edit: Android Studio 3.2 Canary 15 is available for download, everything's working fine for the navigation library. After you've finished installing clear up the clutter invalidating the cache to see if it goes fine.



来源:https://stackoverflow.com/questions/50333357/navhostfragment-not-accessible-from-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!