menu.findItem returns null

Issue

I can’t find the solution please help

The implements:

public class VentasActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks, android.support.v7.widget.SearchView.OnQueryTextListener

where returns null:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        getMenuInflater().inflate(R.menu.sales, menu);
        restoreActionBar();

       // HERE RETURNS NULL
        searchView = (SearchView) menu.findItem(R.id.action_product_search).getActionView();

        if (searchView != null) {
            final Menu menu_block = menu;
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                @Override
                public boolean onQueryTextSubmit(String query) {
                    // collapse the view ?
                    menu_block.findItem(R.id.action_product_search).collapseActionView();
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    // search goes here !!
                    // listAdapter.getFilter().filter(query);
                    return false;
                }
            });
            Log.i("sales module", "SearchView OK");
        } else
            Log.i("sales module", "SearchView is null");


        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

and sales.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/action_product_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="always|collapseActionView"
    android:actionViewClass="android.widget.SearchView" />

Many many thanks!

UPDATED: the code of restoreActionBar()

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

Solution

finally i found the problem:

I changed the last line in my /res/menu/sales.xml from:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="@+id/search_view_productos"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="always|collapseActionView"
    android:actionViewClass="android.widget.SearchView" />
</menu>

to this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="@+id/search_view_productos"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.widget.SearchView" />
</menu>

Problem was the xml namespace on actionViewClass

Answered By – fabrizotus

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published