3

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.

Spittal
  • 779
  • 2
  • 12
  • 23

4 Answers4

6

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

diyoda_
  • 5,274
  • 8
  • 57
  • 89
  • @StarWind what part does not work, I do not know your question – diyoda_ Nov 07 '15 at 08:52
  • Ah its seems to be this app compat stuff. I am trying to use "android:Theme.Material" which will not work with a AppCompatActivity. Which is a shame as I Really need both.. but as I've been at this for hours.. – StarWind0 Nov 07 '15 at 09:02
  • @StarWind add it as a new question of you are having trouble on that – diyoda_ Nov 07 '15 at 09:06
  • I got it figures out. For my future counterpart http://stackoverflow.com/questions/21585326/implementing-searchview-in-action-bar How to do this without the app compat library – StarWind0 Nov 07 '15 at 09:26
0

I had been facing the same problem and none of the solutions provided in the answers worked for me. But this change in the code worked for me. Use

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="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"
appcompat:showAsAction="ifRoom|collapseActionView"
appcompat:actionViewClass="android.support.v7.widget.SearchView" />

Notice the change, "app:actionViewClass" has been changed to " appcompat:actionViewClass".

Worked good for me, hope it works for you too.

  • 1
    Your justification is incorrect. Changing `app` to `appCompat` does nothing. The reason this works is because of `app:actionViewClass="android.support.v7.widget.SearchView"`. I had made it `android:actionViewClass` and thats why it wasn't working for me. – Ali Kazi Jun 19 '17 at 07:48
  • @AliKazi I didn't give any justification, I just shared the solution that worked for me. Though I should have provided a justification. It's all related to the version of SDK you are using. For my version( Sdk version 25 ) the change from "android: " to "app:" didn't work but "android:" to "appcompat:" worked really well. For the version you are using, change from "android" to "app" worked. Even I am unable to understand this diversity in compatibility. – captain.am Jun 19 '17 at 12:27
  • @captain.am your solution did not work originally because you were not referencing the correct namespace attribute here `xmlns:appcompat="http://schemas.android.com/apk/res-auto"`, it's irrelevant if you name it `appcompat` or `appIncompat` – Chisko Jun 26 '17 at 21:57
0

Check the menu Folder in res. in My case Two Files are created First original and 2nd Auto Create with 21. Delete the other file issue resolve if Duplicate happen

-1

You should remove getActivity() from searchManager like below,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
     MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
     SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);

     android.support.v7.widget.SearchView searchView =
            (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);

     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
     searchView.setIconifiedByDefault(false);

}
Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38
  • Hey Thanks for the help, doesn't seem to have worked. Currently this code is inside a fragment because I only want that search to be available when the fragment is on the screen. hence the getActivities. As a test I brought out all the code into the Main Activity itself, and I'm still having the exact same issue, I can find the menu item but I nullpointer when I try to get the actionView. – Spittal Mar 19 '14 at 23:40