PS see the issue before voting down or marking as duplicate.Tried all the possible approaches to hide the menu item but none seems to work.
My options_menu.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="@+id/menu_share"
android:icon="@android:drawable/ic_menu_share"
android:showAsAction="always"
android:title="Share"/>
</menu>
I am displaying this menu on a ParentFragment where I hide the menu_share
using this code
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.options_menu, menu);
menu.findItem(R.id.menu_share).setVisible(false);
}
This hides the Share
option successfully. Now this Fragment opens a new Fragment i.e a Child Fragment.
In this child Fragment, I want to hide the Search
item and show only the Share
option. But using the same code does not help here.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.options_menu, menu);
menu.findItem(R.id.menu_search).setVisible(false);
}
I even tried putting getActivity().invalidateOptionsMenu();
but no effect, the Actionbar shows both the menu items always. Pls help me figure out what is causing this.
ERROR LOG
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.slidingmenu, PID: 27321
java.lang.NullPointerException
at info.androidhive.slidingmenu.HomeFragment.onPrepareOptionsMenu(HomeFragment.java:476)
at android.app.Fragment.performPrepareOptionsMenu(Fragment.java:1794)
at android.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1964)
at android.app.Activity.onPreparePanel(Activity.java:2665)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:540)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:881)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:297)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)