cannot be cast to android.widget.ImageButton

java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.ImageButton

This error occurs when submitting the data.

The application crashes and gets the below error log.

Error log

FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 1794
    java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.ImageButton
        at com.example.crudwithimageupdate.Custom.getView(Custom.java:120)
        at android.widget.AbsListView.obtainView(AbsListView.java:2357)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
        at android.widget.ListView.onMeasure(ListView.java:1182)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5475)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5475)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5475)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:496)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5475)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5475)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5475)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2629)
        at android.view.View.measure(View.java:17456)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2018)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1183)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1389)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1071)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5851)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5240)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)

If you face this type of problem in your Android Studio then this post will help you to solve the error.

There are many reasons for this kind of error in Android Studio. Here the error was coming due to ImageView and ImageButton. Means that here the variable is used elsewhere and cast in ImageView. Hence this type of error is found.

You know exactly the problem when you click on the location link. Just changing the variable and it works.

Finally we can say that this error is getting by casting because by mistake, used variable in ImageButton and casting it to ImageView. Perfect worked after changing the variable. If you are getting that type of error, then check the variable and casting.

Leave a Reply