Null Pointer exception when using SearchView with AppCompat

走远了吗. 提交于 2019-11-30 18:20:23

Maybe it's an obfuscation issue...

Try adding this to your proguard file:

-keep class android.support.v7.widget.SearchView { *; }

I had to do 3 things for this to work:

  1. I went to my proguard-android.txt file found on my machine at Android/sdk/tools/proguard and I pasted the following at the bottom of the file:

-keep class android.support.v7.widget.SearchView { *; }

(then I saved the file)

  1. In my action_menu.xml file in my project found at res/menu I originally had this android:actionViewClass="android.support.v7.widget.SearchView"

but I changed it to this

app:actionViewClass="android.support.v7.widget.SearchView" (the only difference being the word android and app)

So it currently looks like this:

<item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="collapseActionView|ifRoom"
        android:orderInCategory="1"
        app:actionViewClass="android.support.v7.widget.SearchView" />
  1. Then I cleaned my project.

I ran it and it finally worked! No more null pointer exception! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!