SearchView getActionView returning null

痞子三分冷 提交于 2019-11-27 12:34:02

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.

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.

Hope this helps someone.

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 { *; }

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

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

The Android's tutorial, https://developer.android.com/training/search/setup.html, uses android:showAs and android:actionView...

Changing to import android.support.v7.widget.SearchView helped me

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?

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.

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>

I added this too:

setHasOptionsMenu(true);

Working with this and @maxandron answer

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