1

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.

bdls
  • 4,578
  • 6
  • 24
  • 30
  • I don't know if the code in your question is causing the error I copied it into an Activity and it seems to work fine. – JeremyFromEarth May 24 '10 at 17:24
  • Thanks for trying. As you said, it was something other than in the code snippet above that was causing this. I've posted the solution here. – bdls May 25 '10 at 08:47

3 Answers3

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).

bdls
  • 4,578
  • 6
  • 24
  • 30
0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

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.

Community
  • 1
  • 1
Marcus Wolschon
  • 2,550
  • 2
  • 22
  • 28