Android: What could cause an Options Menu inflation error?

☆樱花仙子☆ 提交于 2019-12-11 05:32:55

问题


I am receiving an inflation error when pressing the Menu button and adding a menu item in the onCreateOptionsMenu method. I've included some of the error below.

The code is as follows, and works if I try it on its own and not as part of my activity as a whole. I don't think it's feasible to paste in the whole of my activity here (I have no idea which bit could be causing this), so am wondering if anyone has experienced this before?

public boolean onCreateOptionsMenu(Menu m) {
    Log.d(TAG, "Menu Create");
    this.menu = m;
    m.add(0, 0, 0, "HAZAH!");
    return true;
}

05-24 17:18:47.963: ERROR/AndroidRuntime(1658): android.view.InflateException: Binary XML file line #17: Error inflating class com.android.internal.view.menu.IconMenuItemView 05-24 17:18:47.963: ERROR/AndroidRuntime(1658): at android.view.LayoutInflater.createView(LayoutInflater.java:513) 05-24 17:18:47.963: ERROR/AndroidRuntime(1658): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565) 05-24 17:18:47.963: ERROR/AndroidRuntime(1658): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

05-24 17:18:47.963: ERROR/AndroidRuntime(1658): Caused by: java.lang.reflect.InvocationTargetException 05-24 17:18:47.963: ERROR/AndroidRuntime(1658): at com.android.internal.view.menu.IconMenuItemView.<init>(IconMenuItemView.java:86)

05-24 17:18:47.963: ERROR/AndroidRuntime(1658): Caused by: java.lang.reflect.InvocationTargetException 05-24 17:18:47.963: ERROR/AndroidRuntime(1658): at com.android.internal.view.menu.IconMenuItemView.<init>(IconMenuItemView.java:86)


If I try pressing Menu with the debugger attached I see it stop here:

Suspended (exception InflateException)
ViewRoot.deliverKeyEventToViewHierarchy(KeyEvent, boolean) line: 2425   

Thanks.


回答1:


This was occurring as I had assigned a theme to the Activity which contained the following line:

<item name="android:text"></item>

I was using this to remove the app name text in the title bar. I guess this isn't the right way to do it! Luckily this was one of the recent changes to my app so I was able to track it down (eventually).




回答2:


Don't use 0 for the menu ID. Use Menu.FIRST+1 or something. I would also use Menu.NONE for your other two 0 parameters.

Also, don't return true -- return(super.onCreateOptionsMenu(Menu m)) instead.




回答3:


See How to change the background color of the options menu? if you get this exception because of changing the background of your menu programatically.



来源:https://stackoverflow.com/questions/2898697/android-what-could-cause-an-options-menu-inflation-error

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