NullPointerException异常Android中code异常、NullPointerException、code、Android

2023-09-06 11:11:53 作者:那抹微笑已成殇°

该错误是:

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