menu.findItem returns null

fabrizotus picture fabrizotus · Feb 26, 2014 · Viewed 10.8k times · Source

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);
}

Answer

fabrizotus picture fabrizotus · Mar 2, 2014

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