没有锚点不能使用MenuPopupHelper [英] MenuPopupHelper cannot be used without an anchor

查看:323
本文介绍了没有锚点不能使用MenuPopupHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将PopupMenu添加到我的MenuItem中.

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>

当我点击MenuItem时,我将调用此代码:

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();
}

我得到这个异常:

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

我该如何解决?

推荐答案

我正在阅读互联网",然后尝试以下代码:

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

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

相反

showPopup(item.getActionView());

对我有用

这篇关于没有锚点不能使用MenuPopupHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆