SearchView cannot be cast to android.widget.SearchView

  • Post comments:0 Comments

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

Today we will discuss on android.widget.searchview cannot be cast.

logcat

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 28692
    java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView
        at com.example.crudwithimageupdate.MainActivity2.onCreateOptionsMenu(MainActivity2.java:154)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2888)
        at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:287)
        at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:3084)
        at androidx.appcompat.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1907)
        at androidx.appcompat.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:2188)
        at androidx.appcompat.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:271)
        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)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

How to solve this kind of error.

Everything is fine but when search filter is added then error comes on searching.

May be your error may be different but if you are getting this type of error then you can easily solve.

If you see the error line properly it tells you to cast androidx.appcompat.widget.SearchView  “java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to android.widget.SearchView.

”java.lang.ClassCastException:

Android.appompat.widget.searchview mean that widget you can’t use it. And other like say cannot be cast to android.widget.searchView.

It means that you have imported android.widget.SearchView, you have to use androidx.appcompat.widget.SearchView instead of this.

So, finally, the solution has been found. Replaced androidx.appcompat.widget.searchView instead of android.widget.searchView, filter search started working.

In short, Just go to your menu activity where you have created “onCreateOptionMenu”.

Delete or comment out android.widget.searchView and replace android.appcompat.widget.searchView and run your project. work completely.

If you don’t understand then watch the video.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments