48

This was working a few days ago, but suddenly it stopped. I only want to use the action bar search widget when certain fragment is visible.

Now I'm unable to get the SearchView, now getActionView always returns null.

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_for_places">
</searchable>

Manifest.xml

<activity
    android:name=".ui.activities.MainActivity"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
        <action android:name="android.intent.action.SEARCH"/>
    </intent-filter>
    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable"/>
    <meta-data
    android:name="android.app.default_searchable"
    android:value=".ui.activities.MainActivity" />
</activity>

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:myapp="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search_place"
        android:icon="@drawable/ic_2_action_search"
        android:orderInCategory="1"
        android:title="@string/title_search"
        myapp:showAsAction="collapseActionView|ifRoom"
        myapp:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

The fragment

...
setHasOptionsMenu(true);
...



@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
    inflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search_place);
    mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    SearchManager searchManager = (SearchManager) getBaseActivity().getSystemService(Context.SEARCH_SERVICE);
    if (mSearchView != null) {
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        mSearchView.setIconifiedByDefault(true);
        mSearchView.setOnQueryTextListener(this);
    }
    super.onCreateOptionsMenu(menu, inflater);
}
Axxiss
  • 4,759
  • 4
  • 26
  • 45

9 Answers9

114

I had the same problem because in my menu I used: android:actionViewClass="android.widget.SearchView" as per the google documentation.

Well it turns out that if I'm using AppCompatActivity I should use app:actionViewClass="android.widget.SearchView" instead.

starball
  • 20,030
  • 7
  • 43
  • 238
maxandron
  • 1,650
  • 2
  • 12
  • 16
32

If you're using proguard this is the possible answer: https://code.google.com/p/android/issues/detail?id=58508

In short, add below line to your proguard config file proguard-rules.pro.

-keep class android.support.v7.widget.SearchView { *; }
tomrozb
  • 25,773
  • 31
  • 101
  • 122
  • 1
    Pretty obvious, but I'll still mention - In case you have extended the SearchView to another class, keep the path to that class in proguard! – user2520215 Apr 05 '16 at 14:30
  • Thanks Man . I was facing this issue since 3 days . made all the changes but couldn't identify the bug . This was the problem. – Adi Mar 03 '18 at 08:37
17

Today I had the same problem and I think I solved it. It turns out I did couple of things that were not exactly as per the ActionBarCompat contract:

  • Each activity that uses the ActionBarCompat should extend ActionBarActivity instead of FragmentActivity directly
  • Each activity that uses the ActionBarCompat should declare its theme as inheriting from the ActionBarCompat themes.

Both of those I found watching the explanation video from Google.

Now my searchView is not null anymore.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • 1
    It solved my issue by using theme `Theme.AppCompat` instead of `Theme.Base.AppCompat` – Mehul Joisar Apr 03 '14 at 10:18
  • 36
    My issue was Proguard. If you have Proguard enabled, make sure you have a rule that keeps the `android.support.v7.widget.SearchView`. For example, to keep everything in all the different versions of the support library, add `-keep class android.support.** { *; }` and `-keep interface android.support.** { *; }`. – ashughes May 24 '14 at 00:17
  • 5
    specifying the action class helped my situation: `app:actionViewClass="android.support.v7.widget.ActionMenuView"` – kip2 Apr 27 '15 at 15:48
  • @kip2 the `app:` solved my issue with not using the support.v7 also. Thanks! – Eric B. Aug 01 '16 at 17:51
  • @ashughes Your proguard answer should be theanswer. Thanks heaps! :) – yarrichar May 29 '17 at 04:38
9

I fixed it: in the menu xml, I used the app namespace

For users of androidx libraries:

    app:showAsAction="collapseActionView|always"
    app:actionViewClass="androidx.appcompat.widget.SearchView"

For users of the older support libraries:

    app:showAsAction="collapseActionView|always"
    app:actionViewClass="android.support.v7.widget.SearchView"

The Android tutorial, https://developer.android.com/training/search/setup.html, uses the framework SearchView rather than the androidx/support library one, and so it uses android:showAs and android:actionView

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Alef Carlos
  • 193
  • 2
  • 5
1

Change in the importChanging to import android.support.v7.widget.SearchView helped me

Progga Ilma
  • 578
  • 6
  • 8
0

I was with the same error. And i've made the following things:

I have an Activity extending by FragmentActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.feed, menu);

    // 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 true;
}

Searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable>

Menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="br.com.srtorcedor.FeedActivity">

    <item android:id="@+id/action_search"
        android:icon="@drawable/abc_ic_search"
        android:title="@string/search_title"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView"/>

</menu>

AndroidManifest:

  <activity
        android:name=".FeedActivity"
        android:icon="@drawable/ic_action_pins"
        android:label="@string/title_activity_feed">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

Could you check if it works with you?

Franzé Jr.
  • 1,194
  • 13
  • 20
0

Today I figured out that I was getting null from the getActionView() call on items that did not have:

android:showAsAction="always"

It seems otherwise they just don't inflate correctly.

Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135
0

For me the following solution worked:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.dev.ttburger.conttagem.BuscarProdutoActivity">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:title="Buscar"
        app:showAsAction="always"   
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

Change to:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.dev.ttburger.conttagem.BuscarProdutoActivity">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:title="Buscar"
        app:showAsAction="always"   
        app:actionViewClass="android.SearchView"/>
</menu>
0

I added this too:

setHasOptionsMenu(true);

Working with this and @maxandron answer

n1rocket
  • 25
  • 7