kotlin.TypeCastException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment at NavigationExtensionsKt
Asked Answered
M

8

9

Exception

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

Mafaldamafeking answered 4/5, 2020 at 14:28 Comment(2)
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 and how to create a minimum, reproducible example for better results when using the site. Good luck!Discharge
You can do safe casting using as? operator.Skep
T
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
        )
    }
Tonsillitis answered 14/9, 2020 at 8:44 Comment(0)
A
18

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

Atlantean answered 18/9, 2021 at 15:33 Comment(1)
This was the solution for me too. Thanks!Cranston
T
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
        )
    }
Tonsillitis answered 14/9, 2020 at 8:44 Comment(0)
V
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.

Volnay answered 17/11, 2020 at 12:48 Comment(0)
R
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

Robalo answered 23/6, 2021 at 7:58 Comment(0)
S
1

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.

Squalene answered 11/3, 2022 at 10:37 Comment(0)
Y
1

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

Ypsilanti answered 29/12, 2022 at 8:21 Comment(1)
Same lame thing for meGourami
L
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

Lenssen answered 27/9, 2022 at 19:59 Comment(0)
B
-1

can you try with this?

val selectedFragment: NavHostFragment? = fragmentManager.findFragmentByTag(newlySelectedItemTag) as NavHostFragment
Bullfinch answered 5/5, 2020 at 7:14 Comment(4)
Won't work either cast it to a nullable type or use safe cast operator as?Skep
then what will you do if selectedFragment is null?Bullfinch
wrap it with if elseBullfinch
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 typeSkep

© 2022 - 2024 — McMap. All rights reserved.