MenuPopupHelper cannot be used without an anchor
Asked Answered
F

4

9

I want add PopupMenu to my MenuItem.

Menu.xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/date"
        app:showAsAction="ifRoom|withText"
        android:title="Date"
        android:visible="true"/>
    <item
        android:id="@+id/category"
        app:showAsAction="ifRoom|withText"
        android:title="Category"
        android:visible="true"/>
</menu>

When I click on MenuItem I call this code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.filter_action) {
        showPopup(item.getActionView());
    }
    return super.onOptionsItemSelected(item);
}

private void showPopup(View v) {
    PopupMenu popup = new PopupMenu(getActivity(), v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.filter_billing_menu, popup.getMenu());
    popup.show();
}

And I get this exception:

 java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor

How I can fix it?

Fiasco answered 6/9, 2016 at 10:39 Comment(4)
1. Do you call PopupMenu from Activity or Fragment? 2. Is this menu button visible on screen?Topographer
@DanilOnyanov in fragmentFiasco
Did you check this question #23540292?Topographer
The constructor for PopupMenu needs an anchor view to decide its showing position. You have to pass a non-null view to the second argument for PopupMenu().Christophany
F
22

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

Fiasco answered 6/9, 2016 at 11:1 Comment(1)
Correct answer!Inositol
F
22

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>
Floccus answered 6/9, 2016 at 11:6 Comment(0)
R
2

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();
}
Rodger answered 12/5, 2019 at 18:49 Comment(0)
M
1

Change your this code:

app:showAsAction="ifRoom|withText"

to this:

android:showAsAction="ifRoom|withText"
Meyerhof answered 6/9, 2016 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.