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>
Radwa
  • 325
  • 4
  • 21
  • 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? – Lukáš Anda Sep 29 '22 at 08:23
  • Yes in same hierarchy I edited the question with fragment , yes I added all. – Radwa Sep 29 '22 at 08:31
  • what does your navgraph look like? – Lukáš Anda Sep 29 '22 at 08:38
  • I suspect that the menu items are not defined properly. Feel free to check these links about how to set it up: https://developer.android.com/guide/navigation/navigation-ui#bottom_navigation and https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer – Lukáš Anda Sep 29 '22 at 09:06

0 Answers0