I copied the NavigationExtensions.kt extension file from the NavigationAdvancedSample and I get this exception:
-
4Hi Amir, welcome to SO. Please post the actual text of your code and exception, using correct StackOverflow markdown (instead of images). Also have a look at [**how to ask**](https://stackoverflow.com/help/how-to-ask) and [**how to create a minimum, reproducible example**](https://stackoverflow.com/help/minimal-reproducible-example) for better results when using the site. Good luck! – kenny_k May 04 '20 at 14:33
-
You can do safe casting using `as?` operator. – Animesh Sahu May 04 '20 at 14:35
8 Answers
In my case, I forgot to add android:name="androidx.navigation.fragment.NavHostFragment" in FragmentContainerView

- 178
- 1
- 2
- 7
UPDATE: make sure your menu item ids are the same as your navigation resource files ids
also: if you're having a problem with ItemReselectedListener Add null safety checks, your listener should be something like this:
setOnNavigationItemReselectedListener { item ->
val newlySelectedItemTag = graphIdToTagMap[item.itemId]
val selectedFragment = fragmentManager.findFragmentByTag(newlySelectedItemTag)
as NavHostFragment?
val navController = selectedFragment?.navController
// Pop the back stack to the start destination of the current navController graph
navController?.popBackStack(
navController.graph.startDestination, false
)
}

- 161
- 1
- 7
I also faced this probelem, the solution is actually very simple.
Make sure your menu item's id match the id of NAV_GRAPH not destinations.
I was using the same ids for menu items and destinations, but in this case, menu item should have same id as of NAV_GRAPH.

- 86
- 2
- 5
I manage to solve this problem.
In my case, I was calling val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
before setContentView(binding.root)
so I change the order of the methods and everything was Ok.

- 484
- 5
- 4
I also had the same issue
Check that you have set your start destination and attached it to a fragment. in your nav graph set app:startDestination="@id/your fragment"
this should solve your problem

- 90
- 1
- 3
So for me it was that inside my navigation graph file the id I declared on
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_id"
app:startDestination="@id/container">
....
Was not the same name as the menu item
<item
android:id="@+id/my_idd"
android:enabled="true"
android:icon="@drawable/icon"
android:title="@string/test_title" />
Changing the id to be the same for both solved the problem

- 12,319
- 5
- 67
- 77
I have resolved this, Actually, i set another layout for this activity in setcontentView, When I set the original layout it worked.

- 489
- 3
- 10
can you try with this?
val selectedFragment: NavHostFragment? = fragmentManager.findFragmentByTag(newlySelectedItemTag) as NavHostFragment

- 659
- 7
- 6
-
Won't work either cast it to a nullable type or use safe cast operator `as?` – Animesh Sahu May 05 '20 at 08:02
-
-
-
it will throw the same exception if you try to do like this, it won't be able to cast to a non-null `NavHostFragment` type – Animesh Sahu May 05 '20 at 09:44