AppCompat PopUp menu RuntimeException

妖精的绣舞 提交于 2019-11-29 07:59:22

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.

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

I recommend following solution:

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

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.

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

Good luck!

And feel free to ask.

If you are using Appcompat and lollipop try adding new style

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

and create the popup menu

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

This seems to be a problem of a missing or changed style attribute as indicated by

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)

Internally android looks up style attributes with ?attr/ in the local theme context. Try to debug the code line in which that happens to find out which style is missing.

I found the solution. Only problem was that I had the following declaration in my values-v21/styles.xml

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

So I changed it to

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

and it is working great now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!