I wrote the following code :
public class TabFragment extends Fragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
Log.d("Tab", "SearchManager: " + searchManager + " : " + searchView);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
}
}
----------------------------EDITED-------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Code in menu.xml
<item
android:id="@+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/search_icon"
android:title="Search"
app:showAsAction="always|collapseActionView" />
When I run my code, It throw NullPointerException at
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
It means I got null value at
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
Exceptions are :
java.lang.NullPointerException
at com.ushatek.pustakbazaar.fragment.TabFragment.onCreateOptionsMenu(TabFragment.java:168)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2047)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:2125)
at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:270)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:299)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:243)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4456)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)
I have go through each questions of SO. But am still stuck.
Try with provided inflater instead of
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
this statement may be like following...
inflater.inflate(R.menu.menu_main, menu);
And Second thing is to override onCreate()
of Fragment and write setHasOptionsMenu(true);
It should be like this...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Hope it will help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.