【问题标题】:AppCompat PopUp menu RuntimeExceptionAppCompat 弹出菜单 RuntimeException
【发布时间】:2015-05-11 00:17:07
【问题描述】:

我正在使用以下类来创建图标化弹出菜单:

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 级别

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,而不是 ActionBarActivity。任何想法为什么会产生错误?

感谢转发

编辑:我尝试过使用默认的 appCompat 弹出菜单(所以它没有图标)并且它抛出了同样的错误。有什么想法吗?

【问题讨论】:

  • 我在使用默认 AppCompat v7 弹出菜单时遇到同样的错误
  • 这可能对某些人有帮助:在初始化 PopUpMenu 时只需使用正确的“上下文”,在我的例子中,我使用的是“applicationContext”,然后将其更改为片段的上下文。

标签: android popupmenu


【解决方案1】:

如果您正在使用 Appcompat 和棒棒糖,请尝试添加新样式

<style name="MyPopupMenu" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:popupBackground">#0F213F</item>
    <item name="android:disabledAlpha">0.5</item>
</style>

并创建弹出菜单

Context wrapper = new ContextThemeWrapper(context, R.style.MyPopupMenu);
            PopupMenu popup = new PopupMenu(wrapper, view);

【讨论】:

  • 谢谢,真的帮了我! :)
  • 也帮助了我!谢谢你,你太棒了,你拯救了我的一天!
  • 这是自定义不一定附加到工具栏的弹出对话框的正确解决方案。为我工作,需要一个弹出菜单附加到回收站视图项的溢出按钮。
【解决方案2】:

在这种情况下 MenuPopupHelper.MenuAdapter#getView() 尝试膨胀 ITEM_LAYOUT = R.layout.abc_popup_menu_item_layout 并在此过程中创建 android.support.v7.internal.view.menu.ListMenuItemView 并要求 R.styleable.MenuView(往里找 - declare-styleable name="MenuView")其中索引 6 处的属性是 attr name="android:itemIconDisabledAlpha" 这意味着没有此属性在应用程序主题中定义。在&lt;style name="Theme"&gt; 中为任何平台定义为&lt;item name="disabledAlpha"&gt;0.5&lt;/item&gt; 的必需属性是link

要使用默认值,只需通过扩展 &lt;style name="Theme"&gt; 的主题之一扩展应用程序主题。

我推荐以下解决方案:

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

或任何其他带有 NoActionBar 的 Theme.AppCompat 如果您想将 Toolbar 用于 v21 之前的平台,所有新功能都可作为 ActionBar 组件的替代品。

但如果确实需要,您可以随意使用 Holo 或 Material 等其他主题。

祝你好运!

欢迎提问。

【讨论】:

    【解决方案3】:

    这似乎是样式属性丢失或更改的问题,如

    所示

    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)

    在内部,android 在本地主题上下文中使用?attr/ 查找样式属性。 尝试调试发生这种情况的代码行以找出缺少哪种样式。

    【讨论】:

    • 我完全不知道该怎么做。我的 style.xml 在第 6 行没有任何内容。你能指出我正确的方向吗?我尝试将调试点放在图标化菜单的第 88 行,但我发现了一个异常,但我不知道 idnex 6 是什么意思
    【解决方案4】:

    我找到了解决方案。唯一的问题是我的 values-v21/styles.xml 中有以下声明

    <style name="AppTheme" parent="android:Theme.Material.Light">
    </style>
    

    所以我改成

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

    现在效果很好。

    【讨论】:

      猜你喜欢
      • 2014-08-30
      • 1970-01-01
      • 2011-06-13
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      相关资源
      最近更新 更多