mkandro mkandro - 1 year ago 133
Android Question

NullPointerException in Android code

The error is:


My code is:

42 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
43 SearchView searchView = (SearchView) menu.findItem(;
44 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Log.i("searchView", "set searchable configuration for searchView ");

What could be the problem with my code?

Answer Source

If you're using androids support libary to display your action bar, be sure you use the right actionViewClass in your SearchView declaration.

Should be app:actionViewClass="".

After that you can use as replacement for your code trying to get the ActionView:

MenuItem searchItem = menu.findItem(;
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

Be sure to call getMenuInfalter().inflate(...) before calling this.

More information: