Null Pointer exception when using SearchView with AppCompat

前端 未结 2 1271
野的像风
野的像风 2021-01-04 06:19

Today I have spent the time to migrate to the AppCompat library. I have setup everything successfully except my SearchView.

Before, it functioned properly, but now I

相关标签:
2条回答
  • 2021-01-04 06:46

    Maybe it's an obfuscation issue...

    Try adding this to your proguard file:

    -keep class android.support.v7.widget.SearchView { *; }
    
    0 讨论(0)
  • 2021-01-04 07:00

    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! :)

    0 讨论(0)
提交回复
热议问题