architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

TypeCastException in NavigationExtensions.kt

Open sanisloandras opened this issue 5 years ago • 16 comments

Hello

I am working on a hobby app, using bottom navigation bar. I copied the NavigationExtensions.kt extension file from the NavigationAdvancedSample and I get this exception:

kotlin.TypeCastException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment at com.sanislo.soft.photospot.NavigationExtensionsKt$setupItemReselected$1.onNavigationItemReselected(NavigationExtensions.kt:187) at com.google.android.material.bottomnavigation.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:201) at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840) at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158) at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991) at com.google.android.material.bottomnavigation.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:115) at android.view.View.performClick(View.java:7352) at android.view.View.performClickInternal(View.java:7318) at android.view.View.access$3200(View.java:846) at android.view.View$PerformClick.run(View.java:27800) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7037) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

sanisloandras avatar Aug 02 '19 19:08 sanisloandras

So I figured out that the reason why I was getting this exception because the navigation graphs id in the xml should match the id of the menu items in the bottom navigation menu xml.

I think there should be a red huge warning about this restriction, because I have lost like 5 hours of my life debugging this error...

sanisloandras avatar Aug 03 '19 10:08 sanisloandras

Hey @sanisloandras thanks for posting this issue. I also spent a couple of hours figuring out why I couldn't replicate this sample. Appreciate it!

cgathergood avatar Aug 15 '19 00:08 cgathergood

@sanisloandras thanks you. It is really annoying bug.

VyacheslavMartynenko avatar Sep 09 '19 12:09 VyacheslavMartynenko

@sanisloandras that worked for me too. thank you :)

jeremiahlachica avatar Sep 14 '19 07:09 jeremiahlachica

Thanks @sanisloandras , you save my hours. 👍 :)

aungtuntun avatar Oct 30 '19 11:10 aungtuntun

@sanisloandras you save my day

tidoemanuele avatar Jan 02 '20 18:01 tidoemanuele

kotlin.TypeCastException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment

Getting The Same Issue Please Answer Me Still waiting...

bhargavsavasani avatar Feb 07 '20 16:02 bhargavsavasani

@sanisloandras Thank you so much, you save my hours.

vuthanhict avatar Feb 17 '20 17:02 vuthanhict

This worked for me too. Thank you very much.

ajayjg avatar Apr 27 '20 13:04 ajayjg

@sanisloandras thank you, you save my time. The thing is if you not click bottom navigation item, you even does not realize, because your fragments not show just nothing.

abalta avatar May 21 '20 23:05 abalta

So I figured out that the reason why I was getting this exception because the navigation graphs id in the xml should match the id of the menu items in the bottom navigation menu xml.

I think there should be a red huge warning about this restriction, because I have lost like 5 hours of my life debugging this error...

@sanisloandras Big thanks. Your 5-hour hard work saved me 5 hours 🙂.

therajanmaurya avatar Jul 07 '20 19:07 therajanmaurya

Definitely wasted 2 hours of my life on this... thanks @sasanisloandras for saving the other 3 hours. By the way, how did you come to this solution?

DanielMarantz avatar Aug 31 '20 05:08 DanielMarantz

@DanielMarantz I created a separate test project from scratch, just to see what did I do wrong, and that's how I noticed the mistake.

sanisloandras avatar Aug 31 '20 15:08 sanisloandras

Hello, I have the same error but the ids do match. Screen Shot 2020-12-17 at 3 50 39 PM

what do i do herE?

franquicidad avatar Dec 17 '20 20:12 franquicidad

I had a similar problem, after several hours a discover that I forget to pass the layout as the activity parameter: AppCompatActivity(R.layout.activity_main)

fvaladares avatar Feb 16 '21 00:02 fvaladares

I had a similar problem, after several hours a discover that I forget to pass the layout as the activity parameter: AppCompatActivity(R.layout.activity_main)

this was my error too

khmaies5 avatar May 18 '22 15:05 khmaies5