8

Exception

I copied the NavigationExtensions.kt extension file from the NavigationAdvancedSample and I get this exception:

Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49
Amir
  • 284
  • 1
  • 3
  • 7
  • 4
    Hi 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 Answers8

13

In my case, I forgot to add android:name="androidx.navigation.fragment.NavHostFragment" in FragmentContainerView

Harshal Pudale
  • 178
  • 1
  • 2
  • 7
12

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
        )
    }
tahaak67
  • 161
  • 1
  • 7
5

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.

M Saif Ullah
  • 86
  • 2
  • 5
2

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.

Desilio Neto
  • 484
  • 5
  • 4
1

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

Andrew Ananda
  • 90
  • 1
  • 3
0

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

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

I have resolved this, Actually, i set another layout for this activity in setcontentView, When I set the original layout it worked.

Jadu
  • 489
  • 3
  • 10
-1

can you try with this?

val selectedFragment: NavHostFragment? = fragmentManager.findFragmentByTag(newlySelectedItemTag) as NavHostFragment
Dimitar
  • 659
  • 7
  • 6