Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element ITEM is not allowed in MENU

I'm trying to make a customized options menu. After using this code I get: Element item is not allowed here

Code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    <item android:id="@+id/morsoid_settings"
          android:icon="@drawable/ic_new_game"
          android:title="@string/new_game" />
    <item android:id="@+id/morsoid_close"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>

Inspired by: Android dev guide

like image 465
peter.o Avatar asked Mar 24 '11 21:03

peter.o


2 Answers

I dont know whether it makes a difference, but did you place you menu in res/menu and not in res/layout?

like image 174
Frempe Avatar answered Sep 28 '22 08:09

Frempe


Try leaving out the layout attributes. Here is the example from the documentation:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"
          android:icon="@drawable/ic_new_game"
          android:title="@string/new_game" />
    <item android:id="@+id/help"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>

Edit - also make sure you are using a MenuInflater as the guide suggests:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

Using a LayoutInflater will cause <menu> to be interpreted as a view element, when it is actually a menu resource.

like image 43
Matthew Willis Avatar answered Sep 28 '22 06:09

Matthew Willis