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>