MenuPopupHelper cannot be used without an anchor

I'm reading "internet" and I try this code:

showPopu(getActivity().findViewById(R.id.filter_action));

Instead

showPopup(item.getActionView());

It's works for me


Change your this code:

app:showAsAction="ifRoom|withText"

to this:

android:showAsAction="ifRoom|withText"

My problem was that I had

<item android:id="@+id/menu_entry_to_show_popupmenu"
 app:showAsAction="ifRoom" />

and what I needed is

<item android:id="@+id/menu_entry_to_show_popupmenu"
 app:showAsAction="always" />

showAsAction="always" is needed. A menu entry hidden in the three dots (overflow menu) can't have a popupmenu anchored to it.

My full popupmenu setup function begins like this:

PopupMenu popup = new PopupMenu(getActivity(), getActivity().findViewById(R.id.menu_filter));
    popup.getMenuInflater().inflate(R.menu.filter_tasks, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    [...]
    popup.show();
}

I believe a better (and simpler) approach in this case would be to define a submenu instead of creating a PopupMenu.

For example:

<item android:id="@+id/menu"
    android:title="menu" >
    <menu>
        <item android:id="@+id/item_in_submenu_1"
              android:title="subitem1" />
        <item android:id="@+id/item_in_submenu_2"
              android:title="subitem2" />
    </menu>
</item>