缺少必需的 layout_width 和 layout_height 属性

The required layout_width and layout_height attributes are missing

我有以下布局xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<item android:id="@+id/descriptions_menu"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:icon="@drawable/ic_menu_info_details"
      android:title="Toggle Descriptions On/ Off" />

<item android:id="@+id/settings_menu"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:icon="@drawable/ic_menu_preferences"
      android:title="Settings" />

但我不断收到此错误: 缺少必需的 layout_width 和 layout_height 属性

第 2、9、10 行报告了这一点。这看起来很奇怪。这是我试图复活的一个旧项目(> 5 年)。

我认为 menu 布局在 res/layout 中无效,您需要将其放置在 res/menu 中,如 documentation

中所述

To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements

您可以使用 MenuInflater.inflate()

给它充气

如果这是菜单文件那么你必须关闭文件末尾的菜单标签, 确保此文件位于 res > menu folder > menu file 中。

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <item android:id="@+id/descriptions_menu"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:icon="@drawable/ic_menu_info_details"
          android:title="Toggle Descriptions On/ Off" />

    <item android:id="@+id/settings_menu"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:icon="@drawable/ic_menu_preferences"
          android:title="Settings" />
    </menu>