MenuItemCompat getActionView returns null for compatibility SearchView
I am having problem to get my compatibility SearchView part of my
compatibility ActionBar to work. In the onCreateOptionsMenu of my Activity
I get the searchView as null which causes NullPointerException when I try
to customize it.
Here is what I have setup currently:
This is my menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/search_action_bar_button"
android:icon="@drawable/ic_action_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
</menu>
My activity is declared of the following type:
public class MyActivity extends FragmentActivity
This is the relevant code of my activity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_screen_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search_action_bar_button);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(searchItem);
searchView.setIconifiedByDefault(false); // here searchView is null
which is my problem
return true;
}
I have registered my activity in the AndroidMManifest.xml as searchable
activity, though I am not entirely sure this is needed (I also have
defined the searchable.xml, but will omit posting it for now):
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
I have read the following posts in StackOverflow:
This problem seem to not have been solved
This seems to be solved, but I tried applying the suggested actions as it
can be seen and it did not help me.
I even tried modifying my proguard.config as suggested here, to no avail
(furthermore I think this does not affect development in Eclipse)
No comments:
Post a Comment