AppCompat弹出菜单RuntimeException [英] AppCompat PopUp menu RuntimeException

查看:101
本文介绍了AppCompat弹出菜单RuntimeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下课程创建图标化的弹出菜单:

I am using following class to create iconized popup menu:

public class IconizedMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    private Context mContext;
    private MenuBuilder mMenu;
    private View mAnchor;
    private MenuPopupHelper mPopup;
    private OnMenuItemClickListener mMenuItemClickListener;
    private OnDismissListener mDismissListener;

    /**
     * Callback interface used to notify the application that the menu has closed.
     */
    public interface OnDismissListener {
        /**
         * Called when the associated menu has been dismissed.
         *
         * @param menu The PopupMenu that was dismissed.
         */
        public void onDismiss(IconizedMenu menu);
    }

    /**
     * Construct a new PopupMenu.
     *
     * @param context Context for the PopupMenu.
     * @param anchor Anchor view for this popup. The popup will appear below the anchor if there
     * is room, or above it if there is not.
     */
    public IconizedMenu(Context context, View anchor) {
        mContext = context;
        mMenu = new MenuBuilder(context);
        mMenu.setCallback(this);
        mAnchor = anchor;
        mPopup = new MenuPopupHelper(context, mMenu, anchor);
        mPopup.setCallback(this);
        mPopup.setForceShowIcon(true);
    }

    /**
     * @return the {@link android.view.Menu} associated with this popup. Populate the returned Menu with
     * items before calling {@link #show()}.
     *
     * @see #show()
     * @see #getMenuInflater()
     */
    public Menu getMenu() {
        return mMenu;
    }

    /**
     * @return a {@link android.view.MenuInflater} that can be used to inflate menu items from XML into the
     * menu returned by {@link #getMenu()}.
     *
     * @see #getMenu()
     */
    public MenuInflater getMenuInflater() {
        return new SupportMenuInflater(mContext);
    }

    /**
     * Inflate a menu resource into this PopupMenu. This is equivalent to calling
     * popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).
     * @param menuRes Menu resource to inflate
     */
    public void inflate(int menuRes) {
        getMenuInflater().inflate(menuRes, mMenu);
    }

    /**
     * Show the menu popup anchored to the view specified during construction.
     * @see #dismiss()
     */
    public void show() {
        mPopup.show();
    }

    /**
     * Dismiss the menu popup.
     * @see #show()
     */
    public void dismiss() {
        mPopup.dismiss();
    }

    /**
     * Set a listener that will be notified when the user selects an item from the menu.
     *
     * @param listener Listener to notify
     */
    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
        mMenuItemClickListener = listener;
    }

    /**
     * Set a listener that will be notified when this menu is dismissed.
     *
     * @param listener Listener to notify
     */
    public void setOnDismissListener(OnDismissListener listener) {
        mDismissListener = listener;
    }

    /**
     * @hide
     */
    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
        if (mMenuItemClickListener != null) {
            return mMenuItemClickListener.onMenuItemClick(item);
        }
        return false;
    }

    /**
     * @hide
     */
    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
        if (mDismissListener != null) {
            mDismissListener.onDismiss(this);
        }
    }

    /**
     * @hide
     */
    public boolean onOpenSubMenu(MenuBuilder subMenu) {
        if (subMenu == null) return false;

        if (!subMenu.hasVisibleItems()) {
            return true;
        }

// Current menu will be dismissed by the normal helper, submenu will be shown in its place.
        new MenuPopupHelper(mContext, subMenu, mAnchor).show();
        return true;
    }

    /**
     * @hide
     */
    public void onCloseSubMenu(SubMenuBuilder menu) {
    }

    /**
     * @hide
     */
    public void onMenuModeChange(MenuBuilder menu) {
    }

    /**
     * Interface responsible for receiving menu item click events if the items themselves
     * do not have individual item click listeners.
     */
    public interface OnMenuItemClickListener {
        /**
         * This method will be invoked when a menu item is clicked if the item itself did
         * not already handle the event.
         *
         * @param item {@link MenuItem} that was clicked
         * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
         */
        public boolean onMenuItemClick(MenuItem item);
    }

}

但是,如果我尝试在lolipop上运行它,则会导致错误.它在API级别<21.这是日志记录器:

However if I try to run it on lolipop it causes error. It works fine on devices with API level < 21. Here is the logcat:

02-15 06:08:09.165: E/AndroidRuntime(3824): FATAL EXCEPTION: main
02-15 06:08:09.165: E/AndroidRuntime(3824): Process: hzs.sk.hzs, PID: 3824
02-15 06:08:09.165: E/AndroidRuntime(3824): java.lang.IllegalStateException: Could not execute method of the activity
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.View$1.onClick(View.java:4007)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.View.performClick(View.java:4756)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.View$PerformClick.run(View.java:19749)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.os.Handler.handleCallback(Handler.java:739)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.os.Looper.loop(Looper.java:135)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at java.lang.reflect.Method.invoke(Native Method)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at java.lang.reflect.Method.invoke(Method.java:372)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-15 06:08:09.165: E/AndroidRuntime(3824): Caused by: java.lang.reflect.InvocationTargetException
02-15 06:08:09.165: E/AndroidRuntime(3824):     at java.lang.reflect.Method.invoke(Native Method)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at java.lang.reflect.Method.invoke(Method.java:372)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.View$1.onClick(View.java:4002)
02-15 06:08:09.165: E/AndroidRuntime(3824):     ... 10 more
02-15 06:08:09.165: E/AndroidRuntime(3824): Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 6
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6423)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:679)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:125)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at hzs.sk.hzs.IconizedMenu.show(IconizedMenu.java:88)
02-15 06:08:09.165: E/AndroidRuntime(3824):     at hzs.sk.hzs.MainActivity.showMenu(MainActivity.java:294)
02-15 06:08:09.165: E/AndroidRuntime(3824):     ... 13 more

使用此功能的

activity扩展了Activity,而不是ActionBarActivity.任何想法为什么会产生错误?

activity which uses this extends Activity, not ActionBarActivity. Any ideas why is it generating an error?

感谢前进

编辑:我尝试使用默认的appCompat PopUp菜单(因此它没有图标),并且抛出相同的错误.有什么想法吗?

I have tried using default appCompat PopUp menu (so its without icons) and it is throwing same error. Any ideas?

推荐答案

在这种情况下 MenuPopupHelper.MenuAdapter#getView()尝试为 R.styleable.MenuView (在内部查找-声明样式的名称="MenuView" ),其中索引6的属性为 attr name ="android:itemIconDisabledAlpha" ,这意味着在应用程序主题中未定义此属性.在任何平台的< style name ="Theme"> 中定义为< item name ="disabledAlpha"> 0.5</item> 的必需属性,这是链接.

In this case MenuPopupHelper.MenuAdapter#getView() try to inflate ITEM_LAYOUT = R.layout.abc_popup_menu_item_layout and during this process android.support.v7.internal.view.menu.ListMenuItemView created and require R.styleable.MenuView (look inside to find - declare-styleable name="MenuView") where attribute at index 6 is attr name="android:itemIconDisabledAlpha" this mean that no this attribute defined in application theme. Required attribute defined in <style name="Theme"> for any platform as <item name="disabledAlpha">0.5</item> this is link.

要使用默认值,只需将应用程序主题扩展为扩展< style name ="Theme"> 的主题之一.

To use default value just extend application theme by one of the themes that extends <style name="Theme">.

我建议以下解决方案:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

或任何其他具有NoActionBar的Theme.AppCompat如果您想将工具栏用于具有所有新功能的v21之前的平台可作为ActionBar组件的替换.

or any other Theme.AppCompat with NoActionBar if you want to use Toolbar for older than v21 platforms with all new features available as replacement of ActionBar component.

但实际上,如果确实需要,可以随意使用其他主题,例如Holo或Material.

But actually feel free to use other themes like Holo or Material if it's really required.

祝你好运!

随时问.

这篇关于AppCompat弹出菜单RuntimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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