Reputation: 4838
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.
Upvotes: 11
Views: 22953
Reputation: 1348
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.
Upvotes: 0
Reputation: 40888
For me it was a weird Android Studio bug; and the NavHostFragment
doesn't get resolved until:
NavHostFragment
import import androidx.navigation.fragment.NavHostFragment
Upvotes: 1
Reputation: 99
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
Upvotes: 5
Reputation: 181
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.
Upvotes: 7
Reputation: 9726
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"
}
Upvotes: 0
Reputation: 1
Under the navigation folder, there is an XML file called nav_graph.xml
. Open it and it will prompt Gradle sync.
Project Tree
Upvotes: 0
Reputation: 336
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
Upvotes: 4
Reputation: 12005
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.
Upvotes: 1