android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.41k stars 8.29k forks source link

TypeCastException in NavigationExtensions.kt #693

Open sanisloandras opened 5 years ago

sanisloandras commented 5 years ago

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 commented 5 years ago

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...

cgathergood commented 5 years ago

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!

VyacheslavMartynenko commented 5 years ago

@sanisloandras thanks you. It is really annoying bug.

jeremiahlachica commented 5 years ago

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

aungtuntun commented 4 years ago

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

tidoemanuele commented 4 years ago

@sanisloandras you save my day

bhargavsavasani commented 4 years ago

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

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

vuthanhict commented 4 years ago

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

ajayjg commented 4 years ago

This worked for me too. Thank you very much.

abalta commented 4 years ago

@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.

therajanmaurya commented 4 years ago

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 🙂.

DanielMarantz commented 4 years ago

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?

sanisloandras commented 4 years ago

@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.

franquicidad commented 3 years ago

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?

fvaladares commented 3 years ago

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)

khmaies5 commented 2 years ago

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