11

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.

Nominalista
  • 4,632
  • 11
  • 43
  • 102
  • 1
    Same issue. I think it has something to do with androidx and the support library conflicting but I'm not sure. – der_Fidelis May 14 '18 at 19:00
  • 1
    hi guys! Had the same problem when used recommended by Android Studio FragmentContainerView instead of fragment xml element in layout of main activity "no NavHostFragment found" in mainActivity and in Navigation. I returned to fragment it all works. Gradle 4.0.1 Navigation version 2.3.0 – Leontsev Anton Jul 29 '20 at 23:57

8 Answers8

7

I also just have this headache as I am following Android Kotlin Fundamental tutorial at https://codelabs.developers.google.com/codelabs/kotlin-android-training-add-navigation/index.html#3 Here is the code that works for me

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/myNavHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/navigation"
        app:defaultNavHost="true"/>

Apparently the tutorial has not been updated. Also in the dependency need to update the library ..

....    
implementation "androidx.navigation:navigation-fragment:2.3.0"
implementation "androidx.navigation:navigation-ui:2.3.0"

I am using Android Studio 4.0.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ade Trisna
  • 181
  • 1
  • 6
5

NavHostFragment is not accessible?android:name="androidx.navigation.fragment.NavHostFragment

So in your build.gradle, just add this two dependencies -

For Java:-

dependencies {
def nav_version = "2.3.0"

implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

}

For Kotlin:-

dependencies {
def nav_version = "2.3.0"

implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

}

Reference Link:- https://developer.android.com/jetpack/androidx/releases/navigation

ayush b
  • 99
  • 1
  • 2
4

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

Ilosqu
  • 296
  • 2
  • 6
1

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.

Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
1

For me it was a weird Android Studio bug; and the NavHostFragment doesn't get resolved until:

  • Invlidate Caches and Restart >> Still not resolved
  • Manually add the NavHostFragment import import androidx.navigation.fragment.NavHostFragment
Zain
  • 37,492
  • 7
  • 60
  • 84
0

Under the navigation folder, there is an XML file called nav_graph.xml. Open it and it will prompt Gradle sync.

Project Tree

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Aust L
  • 1
0

Adding dependencies from Android Developers helped me:

dependencies {
   def nav_version = "2.3.0-rc01"

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"

  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
  implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

  // Dynamic Feature Module Support
  implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

  // Testing Navigation
  androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
}
lomza
  • 9,412
  • 15
  • 70
  • 85
0

For me, none of the above answers worked.

val navController = Navigation.findNavController(this, R.id.navHostFragment)

You need to find the navigation controller like this.

Neela
  • 1,328
  • 4
  • 23
  • 45