ITEM not allowed in MENU

I am trying to make a customized options menu. After using this code I get: Element element 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 Developer Guide

+6
source share
4 answers

I do not know if this matters, but have you placed the menu in res / menu, and not in res / layout?

+10
source

Try to keep the layout attributes. Here is an 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 MenuInflater , as the guide suggests:

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

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

+3
source

Not 100% sure that you are talking about a compilation error or about errors displayed on your development system in the layout file.

Using Idea IntelliJ (10.5) , I got this error by pasting the above sample code into the menu.xml file.

However, after creating the project, it disappeared. I still see the layout_width / height errors that you see when editing the menu.xml file, but this does not affect the behavior of the assembly or execution.

0
source

This is kind of an old question, but I decided to put my solution here, since I experienced this in 2019:

I received a warning in Android Studio that said: "The element element is not allowed here" when placing <item> inside <menu> . Turns out my problem was that in my src / main / res / layout folder, and not in my src / main / res / menu folder.

0
source

Source: https://habr.com/ru/post/1345160/


All Articles