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

3

9

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>
Bwana answered 11/1, 2021 at 17:29 Comment(0)
P
9

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'

Procopius answered 11/1, 2021 at 17:39 Comment(0)
P
4

if you are using jetpack compose instead of ComponentActivity to AppCompatActivity

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // compose view code
}}

to

 class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
   //compose view code
}}
Pneuma answered 3/10, 2021 at 8:30 Comment(1)
AppCompatActivity doesn't allow use of setContent { .. }. How do I use both setContent and FragmentContainerView? (Necessary in case of new composable navigation which still uses some old fragments.)Natiha
A
1

In my case I just forgot to add the startDestination fragment inside the navigation file:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/navigation_graph"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/nav_main">
Anastasiaanastasie answered 14/8, 2021 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.