Java – Search widget, NullPointer?

Search widget, NullPointer?… here is a solution to the problem.

Search widget, NullPointer?

Hello, I’m having trouble implementing the search widget in my app. It can’t seem to find the “actionview” of the menu item, but it can find the item well.

I looked around for answers but didn’t see a clear solution.

This is the menu I declared 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" />

Here’s how it’s implemented.

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()));

This is the library I added

, maybe I added the wrong library?

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

Finally, the crash log

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)

I’ve followed the docs for T, but still can’t figure out what the problem is.

Solution

I had the same issue today and solved the confusion below

I also used the following dependencies

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

This means that I am extending the Activity class from AppCompatActivity instead of the Activity class.

So in this code,

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

This should be android.support.v7.widget.SearchView instead of android.widget.SearchView. That’s why you get it

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

Also,

<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" />

Please note that
It is, app:actionViewClass is not android:actionViewClass

Related Problems and Solutions