SearchView getActionView returning null

29.8k views Asked by At

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);
}
9

There are 9 answers

5
Boris Strandjev On BEST ANSWER

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.

2
tomrozb On

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 { *; }
0
Pedro Gabriel Lancelloti Pinto On

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
Progga Ilma On

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

0
Franzé Jr. On

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?

0
n1rocket On

I added this too:

setHasOptionsMenu(true);

Working with this and @maxandron answer

2
maxandron On

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.

0
Daniel Wilson On

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.

0
Alef Carlos On

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