I have an actionbar with a menu. When you click the button, the menu shows up and all is well with the world.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/reload"
android:title="@string/reload"
android:alphabeticShortcut="r"
android:numericShortcut="1"
android:titleCondensed="Rload"></item>
<group android:id="@+id/adv" android:menuCategory="secondary">
...options
</group>
</menu>
However, I just got told that we have a client whose device has some sort of other menu bar that is displayed over the current one so they can't access the menu. So I added a button on the webview with a javascriptinterface to tell my Activity to display the menu programmatically. I can get there just fine, but I can't for the life of me figure out how to display the menu as if the menu button was pressed.
I tried doing something like this:
private void showMenu() {
try{
actionbar.show();
MenuItem menu = (MenuItem) findViewById(R.id.adv);
menu.expandActionView();
}
catch(Exception e){
Log.e(LOG_CAT,"Unable to inflate menu");
}
}
but I'm getting a null pointer exception
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.MenuItem.expandActionView()' on a null object reference
It seems like what I'm trying to do should be trivially easy, but it may be that I'm just doing something boneheaded instead.
EDIT I don't actually want to just show R.id.adv, I would like to show the whole menu so that should probably be findViewById(R.menu.menu_main)
instead, where menu_main
is the name of the file that the above xml is defined in.
findViewById
looks in the view hierarchy of the activity, not the menu. You need to get the items you need right after your menu is created:Edit:
Didn't think it's possible but it is. With the help of this answer here's how you can do it:
And in your
styles.xml
:findViewsWithText
requires API 14+, however the answer I linked, has a solution for lower APIs.