NullPointerException异常Android中code [英] NullPointerException in Android code
本文介绍了NullPointerException异常Android中code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该错误是:
java.lang.NullPointerException.onCreateOptionsMenu(AddMyMarketsActivity.java:44)
我的code是:
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 ");
有什么能与我的code中的问题?
What could be the problem with my code?
推荐答案
如果您在使用机器人支持libary,以显示你的动作条,一定要在你的搜索查看声明中使用正确的actionViewClass。
If you're using androids support libary to display your action bar, be sure you use the right actionViewClass in your SearchView declaration.
应应用:actionViewClass =android.support.v7.widget.SearchView
之后,你可以作为替代使用您的code试图获得而ActionView:
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);
在调用此之前一定要打电话getMenuInfalter()。膨胀(...)。
Be sure to call getMenuInfalter().inflate(...) before calling this.
更多信息: http://developer.android.com/guide /topics/ui/actionbar.html#ActionView
这篇关于NullPointerException异常Android中code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文