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.