Android SearchView does not work

£可爱£侵袭症+ 提交于 2019-12-01 03:20:48

For API before 11 you should initialize Action Bar Items in compatibility mode:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchMenuItem);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

Update:
So, I tried to reproduce it in API 8 emulator and on 4.4 KitKat. Unfortunately, my 2.3.8 device got bricked a while ago, so could not check in hardware with low API. What I can suggest you:
1. Check that SearchView is imported from android.support.v7.widget.SearchView;
2. Check menu resource is correct:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" <----------- init compatible namespace
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >

    <item
        android:id="@+id/action_showTC"
        android:orderInCategory="100"
        android:title="@string/terms_and_conditions"
        app:showAsAction="never"/>  <----------- use compatible namespace

    <item android:id="@+id/action_search"
          android:title="@string/search"
          android:orderInCategory="100"  <---------- use different value
          app:showAsAction="ifRoom|collapseActionView"
          app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

3. return true from onCreateOptionsMenu if there's no underlying processing (Activity class is not subclassed)

UPDATE2: GOT IT! You probably extends from Activity class. Should be ActioBarActivity:

public class MainActivity extends ActionBarActivity {
...

Add following code to proguard-rules.pro

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

Have you enabled Proguard in your build? If so, you may want to ensure that the appcompat libraries are in the Proguard exclusion list (in proguard.cfg). A brute force approach is to keep all the support library classes with:

   -keep class android.support.v4.app.** { *; }
   -keep interface android.support.v4.app.** { *; }
   -keep class android.support.v7.app.** { *; }
   -keep interface android.support.v7.app.** { *; }

In my case, I had a class that extended the support library's SearchView so I added this to my proguard.cfg:

-keep public class * extends android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

The constructors are specifically mentioned to avoid the error:

java.lang.NoSuchMethodException: <init> [class android.content.Context]

After long hours of research a simple solution of this problem i.e just add

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

in app/proguard/android.proguard file of AndroidStudio.

Cheers!

I (also) got

Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.ao)' on a null object reference

when launching my release build (with proguard/minify enabled).

Adding this to the proguard rules fixed it:

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

This will keep the SearchView widget but will still allow proguard to throw away any other support library classes that you're not using, so you keep your release build nice and tidy.

Required minimum to proguard-rules.pro

-keep class android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

I had to add this and now it works smooth:

-keep class android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}
-keep interface android.support.v7.widget.SearchView {
   public <init>(android.content.Context);
   public <init>(android.content.Context, android.util.AttributeSet);
}

For AndroidX searchview proguard you have to use:

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