0

I am using android Bottom Navigation and gettimg this crash Caused by java.lang.IllegalStateException Fragment androidx.navigation.fragment.NavHostFragment did not create a view.

Here is my setup method

  fun setUpNavigation() {
        val navView: BottomNavigationView = binding.navView
        navHostFragment = (supportFragmentManager
                .findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment?)!!
        NavigationUI.setupWithNavController(
                navView,
                navHostFragment!!.navController
        )
     
    }

and xml

 <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:theme="@style/BottomNavigationTheme"
        app:itemBackground="@drawable/bottom_nav_indicator"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_nav_menu" />
  <fragment
    android:id="@+id/nav_host_fragment"
    class = "androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/nav_view"
    app:navGraph="@navigation/mobile_navigation" />

and this is how my navGraph looks like

<?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/mobile_navigation"
    app:startDestination="@+id/navigation_first">

    <fragment
        android:id="@+id/navigation_first"
        android:name="com.myapp.firstFragment"
        android:label="@string/firstFragment"

        />

    <fragment
        android:id="@+id/navigation_second"
        android:name="com.myapp.secondFragment"
        android:label="@string/secondFragment"
        />
    <fragment
        android:id="@+id/navigation_third"
        android:name="com.myapp.thirdFragment"
        android:label="@string/thirdFragment"
        />
    <fragment
        android:id="@+id/navigation_fourth"
        android:name="com.myapp.fourthFragment"
        android:label="@string/fourthFragment"

        />
    
</navigation>
4
  • Is your fragment present in the same view hierarchy? Does it have navigation set properly? Do you have added all the fragments that the bottomnavigation requires? Commented Sep 29, 2022 at 8:23
  • Yes in same hierarchy I edited the question with fragment , yes I added all. Commented Sep 29, 2022 at 8:31
  • what does your navgraph look like? Commented Sep 29, 2022 at 8:38
  • I suspect that the menu items are not defined properly. Feel free to check these links about how to set it up: developer.android.com/guide/navigation/… and developer.android.com/guide/navigation/… Commented Sep 29, 2022 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.