Search code examples
androidkotlinnavigationnav

Trying to use NavHostFragment and im getting an error: Error inflating class androidx.fragment.app.FragmentContainerView


here is the caused by: " Caused by: android.view.InflateException: Binary XML file line #21 in com.example.dnaire:layout/main: Binary XML file line #21 in com.example.dnaire:layout/main: Error inflating class androidx.fragment.app.FragmentContainerView Caused by: android.view.InflateException: Binary XML file line #21 in com.example.dnaire:layout/main: Error inflating class androidx.fragment.app.FragmentContainerView Caused by: java.lang.reflect.InvocationTargetException"

I don't have much code yet in the project, just started it.

here is the Main activity:

class Main : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mainBinding = MainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)
    }
}

that is the main.xml file that contains the FragmentContainerView that causes the problem:

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

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

that is the nav_graph.xml file

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph"
    app:startDestination="@id/startFragment">

    <fragment
        android:id="@+id/startFragment"
        android:name="com.example.dnaire.login.StartFragment"
        android:label="fragment_start"
        tools:layout="@layout/fragment_start" />
</navigation>

Solution

  • Your actvity must be a child of FragmentActivity to use androidx.fragment.app.FragmentContainerView either you extend the FragmentActivity or the AppCompatActivity which is a child FragmentActivity class. It is recommended to extend the AppCompatActivity.

    gradle dependency for AppCompat in jectpack implementation 'androidx.appcompat:appcompat:1.2.0'