The error is:
java.lang.NullPointerException.onCreateOptionsMenu(AddMyMarketsActivity.java:44)
My code is:
41 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
42 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
43 SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
44 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Log.i("searchView", "set searchable configuration for searchView ");
What could be the problem with my code?
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="android.support.v7.widget.SearchView"
.After that you can use as replacement for your code trying to get the ActionView:
Be sure to call getMenuInfalter().inflate(...) before calling this.
More information: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
I had the same error and it was removed by changing:
to
from menu.xml file
I'm describing the whole procedure of adding search view to action bar.This snippets of code worked for me.
Add search view in menu:add the following item in the menu layout of the activity in which you require search view.
Note: menu layout file should contain following schema. xmlns:app="http://schemas.android.com/apk/res-auto"
Create a Searchable Configuration: create a file searchable.xml in the directory res/xml. copy the following code into it.
Modify onCreateOptionsMenu() method: In the onCreateOptionsMenu() method of your activity, associate the searchable configuration with the SearchView by calling setSearchableInfo(SearchableInfo): Your onCreateOptionsMenu should look like this.
Make an activity SearchResultsActivity to show the results of your search. In the manifest file ,add the following
Your SearchResultsActivity should implement the following methods
Note:You should call the following from onCreate method of SearchResultsActivity