搜索小插件,空指针? [英] Search Widget, NullPointer?

查看:211
本文介绍了搜索小插件,空指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI'm有实现我的应用程序中选择搜索插件的问题。它似乎不能够找到的菜单项的ActionView,但它是找到项目就好了。

HI'm having an issue implementing the search widget in my application. It seems to not be able to find the "actionview" of the menu item, but it is finding the item just fine.

我环顾四周的答案并没有看到一个明确的解决方案。

I've looked around for answers and haven't seen a clear cut solution.

下面是我在声明XML的菜单

Here's the menu I'm declaring in XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/group_search_box"
    android:title="@string/search_label"
    android:icon="@drawable/ic_action_action_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

这是它是如何实现的。

SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.group_search_box);
    SearchView searchView = (SearchView) searchMenuItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

下面是我添加的库,也许我添加了不正确的?

Here are the libraries I'm adding, maybe I'm adding an incorrect one?

compile 'com.android.support:support-v4:+'
compile 'com.android.support:appcompat-v7:+'

和终于在这里是崩溃日志

and finally here is the crash logs

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
        at com.example.myapp.fragments.GroupFragment.onCreateOptionsMenu(GroupFragment.java:88)
        at android.app.Fragment.performCreateOptionsMenu(Fragment.java:1780)
        at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1927)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2539)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
        at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
        at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

我跟着文档到T和仍然无法弄清楚的问题。

I've followed the docs to a T and still can't figure out the issue.

推荐答案

我面临着同样的问题今天摆脱了混乱如下,

I faced the same issue today and got rid of the confusion as follows,

我也是用下面的依赖性,

I am also using the following dependencies,

compile 'com.android.support:support-v4:+'
compile 'com.android.support:appcompat-v7:+'

这意味着我从 AppCompatActivity 扩展活动类,而不是活动类。

Which means I am extending the activity class from AppCompatActivity rather than Activity class.

因此​​,在这个code,

So in this code,

SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

本,搜索查看应该是, android.support.v7.widget.SearchView 不是 android.widget.SearchView 。这是你在​​这的原因,

this, SearchView should be, android.support.v7.widget.SearchView not android.widget.SearchView. This is the reason you got,

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference

和也,

<item
        android:id="@+id/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@android:drawable/ic_search_category_default"
        android:title="Search"
        app:showAsAction="always|collapseActionView" />

公告称,
它是,应用:actionViewClass 不会 的android:actionViewClass

这篇关于搜索小插件,空指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆