64

Since I implented app compat my searchview doesn't work anymore:

 Process: com.laurenswuyts.witpa, PID: 26666
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
            at com.laurenswuyts.witpa.Activities.Events.EventActivity.onCreateOptionsMenu(EventActivity.java:75)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2820)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
            at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
            at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
            at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)
            at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
            at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
            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:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

So nullpointer for searchview while I have it:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.event_main, menu);

        // Get the SearchView and set the searchable configuration
        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));


        return super.onCreateOptionsMenu(menu);
    }

And in my menu I have this:

<!-- Search Widget -->
  <item android:id="@+id/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/ic_action_search"
      app:showAsAction="always"
      android:actionViewClass="android.support.v7.widget.SearchView"/>

I have no idea why it doesn't work anymore but it happened since I started using app compat 21.

Regards,

Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39

11 Answers11

235

Try using the custom app namespace for your actionViewClass too:

app:actionViewClass="android.support.v7.widget.SearchView"/>
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Wow that worked, can u explain to me why I have to use app instead of android? – Laurenswuyts Nov 20 '14 at 20:57
  • 2
    @Laurenswuyts since you started using the support library, some attributes now have to be defined with a custom namespace. You have that namespace defined in your menu file. Some more info can be found in [the docs](http://developer.android.com/training/custom-views/create-view.html#customattr). – Simas Nov 20 '14 at 21:02
  • 10
    I use app: namespace, and it worked on debug version, but it causes the same error in release mode when I have minifyEnabled true in gradle.build, any suggestion? – Ashraf Alshahawy Jan 10 '16 at 17:03
  • 2
    @ Ashraf Alshahawy ...use -keep class android.support.** { *; } in progaurd-rules in case of minifyEnabled true – Brijesh Kumar Feb 27 '17 at 05:51
40

Add the following line to proguard-rules.pro file located inside app folder:

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

Darush
  • 11,403
  • 9
  • 62
  • 60
15

This could also happen if you have proguard enabled and it is striping aways SearchView class. You would need to modify proguard settings to keep the class.

See this question for more details.

Community
  • 1
  • 1
Parag Sarda
  • 1,576
  • 1
  • 13
  • 5
12

It´s an additional information to Simas answer. I found this in another answer (https://stackoverflow.com/a/33400808/4949671) and it was very important for solving my exception:

Notice that, it is, app:actionViewClass NOT android:actionViewClass

Community
  • 1
  • 1
10

after a while of playing "run & error" I found a solution.. Looks like the UI-element isn't the reason what causes the error. After setting the search to the QueryListener it is working well. Here is some code:

Activity which contains the SearchBar:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                Log.d(TAG, "onQueryTextSubmit ");
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                Log.d(TAG, "onQueryTextChange ");
                return false;
            }
        });

        return true;
    }

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
                                      android:label="@string/app_name"
                                      android:hint="@string/search_hint"/>

And the "SearchBar" in menu.xml

     <item
            android:id="@+id/search"
            android:title="@string/search_title"
            android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
            app:showAsAction="collapseActionView|ifRoom"
            app:actionViewClass="android.support.v7.widget.SearchView"/>

...

And last, but not least, the manifest (but this should be clear)...

  <activity
        android:name=".activities.MainActivity"
        android:label="@string/title_activity_main">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>

more information -> Docs

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
4

If you use progurd then you have to add the following line into proguard-rules.pro file

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

or

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

Masum
  • 4,879
  • 2
  • 23
  • 28
1

If if you minify in your build types like that then you need to add a single line your 'proguard-rules.pro' file.

buildTypes {
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Add line your 'proguard-rules.pro' file.

-keep class android.support.v7.widget.SearchView { *; }
Atiar Talukdar
  • 668
  • 11
  • 22
0

After you've clicked on a result, your app is expecting that an operation has not completed and is trying to go further into an Intent argument.

SearchView.OnSuggestionListener and return true which notifies your app that the click operation has completed.

        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {

            @Override
            public boolean onSuggestionClick(int position) {
                return true;
            }

            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }
        });
0

Issue occurs if Proguard is enabled. can be fixed by adding this to proguard rules

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

0

I had pretty similar issue with androidx.appcompat.widget.SearchView, but different cause. The crash occurs on an action button click

 Process: hotspotshield.android.vpn, PID: 26089
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
    at androidx.appcompat.widget.SearchView.adjustDropDownSizeAndPosition(SearchView.java:1373)
    at androidx.appcompat.widget.SearchView$4.onLayoutChange(SearchView.java:380)
    at android.view.View.layout(View.java:20690)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)

The cause is missing attribute android:popupBackground in the theme. Actually material AppCompat themes has the required background, but despite my theme extends from Theme.AppCompat.Light, it still crashes. The only solution i have found is to override the attribute:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:popupBackground" tools:ignore="PrivateResource">@drawable/abc_popup_background_mtrl_mult</item>
</style>

It is the default drawable which is used in AppCompat. It is marked as private, but you may use it though. Or declare background drawable by yourself

Beloo
  • 9,723
  • 7
  • 40
  • 71
0

in my case, this issue occur after migrate my project to AndroidX. so replace this: android.support.v7.widget.SearchView by this: androidx.appcompat.widget.SearchView

hamil.Dev
  • 137
  • 2
  • 9