Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in Android code

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?

like image 810
mkandro Avatar asked Apr 07 '26 17:04

mkandro


2 Answers

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:

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

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

More information: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

like image 187
Anonfunc Avatar answered Apr 09 '26 07:04

Anonfunc


I had the same error and it was removed by changing:

android:actionViewClass="android.widget.SearchView"

to

app:actionViewClass="android.widget.SearchView"

from menu.xml file

i.e change android: to app:

like image 35
Rana Shahid Avatar answered Apr 09 '26 08:04

Rana Shahid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!