NavHostFragment not accessible from XML

Nominalista picture Nominalista · May 14, 2018 · Viewed 12.2k times · Source

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.

Answer

Ilosqu picture Ilosqu · May 17, 2018

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