问题
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);
}
回答1:
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 ofFragmentActivity
directly - Each activity that uses the
ActionBarCompat
should declare its theme as inheriting from theActionBarCompat
themes.
Both of those I found watching the explanation video from Google.
Now my searchView
is not null
anymore.
回答2:
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.
回答3:
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 { *; }
回答4:
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...
回答5:
Changing to import android.support.v7.widget.SearchView helped me
回答6:
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?
回答7:
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.
回答8:
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>
回答9:
I added this too:
setHasOptionsMenu(true);
Working with this and @maxandron answer
来源:https://stackoverflow.com/questions/18407171/searchview-getactionview-returning-null