SearchView getActionView returning null
Asked Answered
R

9

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);
}
Rebeccarebecka answered 23/8, 2013 at 16:1 Comment(0)
M
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.

Minne answered 1/9, 2013 at 11:6 Comment(5)
It solved my issue by using theme Theme.AppCompat instead of Theme.Base.AppCompatUniversalism
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.** { *; }.Caldeira
specifying the action class helped my situation: app:actionViewClass="android.support.v7.widget.ActionMenuView"Rayfordrayle
@Rayfordrayle the app: solved my issue with not using the support.v7 also. Thanks!Engud
@Caldeira Your proguard answer should be theanswer. Thanks heaps! :)Buoyant
G
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.

Glutenous answered 10/9, 2015 at 8:56 Comment(2)
I used app:actionViewClass="android.support.v7.widget.SearchView"Labors
Lifesaver! Been searching for hours! Hopefully, they update their docs soon. Usually, their docs are pretty good.Conservation
T
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 { *; }
Tannie answered 14/11, 2014 at 19:41 Comment(2)
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!Celin
Thanks Man . I was facing this issue since 3 days . made all the changes but couldn't identify the bug . This was the problem.Gallego
M
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.

Minne answered 1/9, 2013 at 11:6 Comment(5)
It solved my issue by using theme Theme.AppCompat instead of Theme.Base.AppCompatUniversalism
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.** { *; }.Caldeira
specifying the action class helped my situation: app:actionViewClass="android.support.v7.widget.ActionMenuView"Rayfordrayle
@Rayfordrayle the app: solved my issue with not using the support.v7 also. Thanks!Engud
@Caldeira Your proguard answer should be theanswer. Thanks heaps! :)Buoyant
M
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

Mckenney answered 25/10, 2016 at 3:20 Comment(0)
H
1

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

Headway answered 27/5, 2018 at 20:33 Comment(0)
P
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?

Petard answered 31/7, 2014 at 1:10 Comment(0)
W
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.

Wellknown answered 3/2, 2015 at 12:32 Comment(0)
A
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>
Axiology answered 15/12, 2017 at 16:20 Comment(0)
S
0

I added this too:

setHasOptionsMenu(true);

Working with this and @maxandron answer

Southwester answered 20/12, 2018 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.