ptia
ptia

Reputation: 141

android search widget menu item

I use this code as this Android API Guides suggest to have a search widget in android 3.0 or more:

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the options menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    return true;
}

I have declared search menu item (and other menu item) in res/menu/menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/help"
        android:icon="@android:drawable/ic_menu_help"
        android:title="Help" 
        />
    <item android:id="@+id/menu_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search" 
        android:showAsAction="ifRoom"
    />
</menu>

I also created an activity for search results and its res/xml/searchable.xml settings, as API Guides suggest. But as I start my app i get a NullPointerException atonCreateOptionsMenuand at onCreatePanelMenu

Upvotes: 1

Views: 4185

Answers (1)

Buneme Kyakilika
Buneme Kyakilika

Reputation: 1202

As @A--C said, You forgot that your menu_search item needs android:actionViewClass="android.widget.SearchView Try adding that and see if it works. If not, make sure to attach your LogCat stack trace as an edit to your question.

Upvotes: 1

Related Questions