Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Widget, NullPointer?

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.

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

And here is how it is 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()));

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)

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

like image 809
Spittal Avatar asked Feb 03 '26 18:02

Spittal


1 Answers

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:+'

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

So in this code,

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

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

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

Notice that, it is, app:actionViewClass NOT android:actionViewClass

like image 65
diyoda_ Avatar answered Feb 05 '26 08:02

diyoda_



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!