Error inflating class Fragment, when adding navGraph attribute
Asked Answered
C

4

6

I know there are a few questions and answers about this problem, but none of the solutions fixed my problem. I get an

android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class fragment

error, when I add the navGraph attribute with the navigation xml-file.


So here is my activity_login.xml (with the fragment container):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">

<fragment
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:id="@+id/loginFragmentContainer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"

    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"

    app:navGraph="@navigation/nav_graph"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The nav_graph.xml is just as empty as it is generated.


Here are the important dependencies for the fragment:

implementation "androidx.navigation:navigation-fragment:2.3.0"
implementation "androidx.navigation:navigation-ui:2.3.0"
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
implementation "androidx.navigation:navigation-dynamic-features-fragment:2.3.0"
androidTestImplementation "androidx.navigation:navigation-testing:2.3.0"
implementation "androidx.fragment:fragment:1.2.5"
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"

When I do not include this line app:navGraph="@navigation/nav_graph" in my activity_login.xml -layout, I do not get an error. What's my mistake?

I am using Android Studio 4.1 Canary 10

Please just ask if I need to share more code or information... Thanks in advance and best regards!

Cowden answered 2/7, 2020 at 20:0 Comment(6)
And of course inside your "res" directory is a "navigation" directory? and it contains nav_graph.xml file, which when you double click loads correctly in the android IDE?Galactometer
Is your nav_graph.xml really just empty? That won't work at all - you'd need to actually add a destination to it before you can use it. Please include it and the error message you're getting.Catafalque
@DavidKroukamp , yes, there is this directory and it loads correctly on Ctrl+Left-ClickCowden
@Catafalque , the "code" for this xml is just the standart code - no destination or anything. Do I need to implement that? I have no experience with thode NavGraphs, but when I firstly have to do that, I know where to start. <?xml version="1.0" encoding="utf-8"?> <navigation xmlns:android="schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto" android:id="@+id/nav_graph"> </navigation>Cowden
The error message should specificially say you need a startDestination, so yes, you need to add one of those :) Sounds like going through the Getting Started guide or the Navigation codelab might be helpful.Catafalque
@Catafalque so stupid of me... I did just read the inflating error, after that there is no start destination defined via app:startDestination for com.example.{APPNAME}:id/nav_graph Thanks a lot! You can answer the post with this solution if you wish, I will mark it as correct.Cowden
C
3

Your navigation graph needs to have at least one destination - the start destination of your graph and the first screen users see when you inflate your graph as per the Getting Started guide.

Catafalque answered 2/7, 2020 at 21:12 Comment(0)
H
7

After updating to 'androidx.appcompat:appcompat:1.4.0' I received the same error.

What worked for me was to change:

  • in activity layout xml file from <fragment node to <androidx.fragment.app.FragmentContainerView

before

<fragment
    android:id="@+id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
...
    app:navGraph="@navigation/nav_graph"/>

after

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
...
    app:navGraph="@navigation/nav_graph"/>
  • in activity Class file how the navController is initialized

before

        navController = findNavController(R.id.navHostFragment)

after

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
        navController = navHostFragment.navController
Halliday answered 7/1, 2022 at 13:16 Comment(1)
this works. what is weird is that this only caused my app to fail sometimes.Arterial
C
3

Your navigation graph needs to have at least one destination - the start destination of your graph and the first screen users see when you inflate your graph as per the Getting Started guide.

Catafalque answered 2/7, 2020 at 21:12 Comment(0)
S
0

To navigation, you just need to implement two dependencies:

implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
implementation "android.arch.navigation:navigation-ui-ktx:$version_navigation"

Please, share your nav_graph.xml

Stork answered 2/7, 2020 at 20:40 Comment(0)
A
0

For anyone still struggles with this problem can add this to proguard-rules.pro

-keepnames class * extends android.os.Parcelable
-keepnames class * extends java.io.Serializable
Alyosha answered 15/10, 2023 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.