Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating class Fragment, when adding navGraph attribute

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!

like image 244
Malmatth Avatar asked Jul 24 '26 05:07

Malmatth


2 Answers

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
like image 126
Nena Schmidt Avatar answered Jul 27 '26 07:07

Nena Schmidt


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.

like image 42
ianhanniballake Avatar answered Jul 27 '26 06:07

ianhanniballake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!