Skip to content
Advertisement

BottomNavigation, FragmentManager has not been attached to a host

I have moved BottomNavigation codes from MainActivity to A class I have created for the BottomNaviction to make the code more organizing. When I moved the codes I got this error java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moataz.mox/com.moataz.mox.ui.view.activity.MainActivity}: java.lang.IllegalStateException: FragmentManager has not been attached to a host. Short error message FragmentManager has not been attached to a host.

And The error In this line of code

JavaScript

Here’s My BottomNavigation Class

JavaScript

And here I haved Called my class and method In MainActivity

JavaScript

I have tried to find a sloution and anderstand the error but I didn’t found sothing In my case. So what is the problem here and How can I fix It?

Advertisement

Answer

You cannot make a new instance of an activity; activity instances are built only by Android itself. The only thing you can do in order to obtain an activity reference is to use an existing one.

JavaScript

And pass your existing reference of your activity:

JavaScript

Note that you still need to attach your BottomNavigation to a view parent in the activity to make it visible.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement