Skip to content Skip to sidebar Skip to footer

Android.widget.toolbar Cannot Be Cast To Androidx.appcompat.widget.toolbar Even Though That Is Not The Case

Im not sure why i am getting this error. I am not casting from two different toolbars Here is my code: androidx.appcompat.widget.Toolbar toolbar = (androidx.appcompat.widget.Too

Solution 1:

Wrong Toolbar class defined in your xml file. Change it from

<Toolbar.../>

to

<androidx.appcompat.widget.Toolbar.../>

Solution 2:

In your XML, you probably declared your toolbar using just <Toolbar></Toolbar> in that case, the toolbar will be created from the package android.widget. So if you try to call findViewById by casting it to androidx.appcompat.widget.Toolbar it will surely throw you a RuntimeException.

If you are using AndroidX, which you should, then you have to change the xml declaration of your toolbar to <androidx.appcompat.widget.Toolbar></androidx.appcompat.widget.Toolbar>

You can then proceed to call your (androidx.appcompat.widget.Toolbar) findViewByid(..) which should succeed

Post a Comment for "Android.widget.toolbar Cannot Be Cast To Androidx.appcompat.widget.toolbar Even Though That Is Not The Case"