Android: What could cause an Options Menu inflation error?
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.
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).
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.
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.
精彩评论