【问题标题】:Android | The following classes could not be found: menu and item安卓 |找不到以下类:菜单和项目
【发布时间】:2013-09-02 17:34:43
【问题描述】:

我试图在我的应用程序上放置一个菜单,但是当我在我的 xml 中放置一个菜单标签时:

<menu
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <item
        android:id="@+id/item1"
        android:title="Item 1">
    </item><item
        android:id="@+id/item2"
        android:title="Item 1">
    </item>
    <item
        android:id="@+id/item3"
        android:title="Item 1">
    </item>
</menu>

我在进行图形布局时收到此错误消息。

java.lang.ClassCastException
Exception details are logged in Window > Show View > Error Log
The following classes could not be found:
- item (Fix Build Path, Edit XML)
- menu (Fix Build Path, Edit XML)

有人知道为什么吗?

【问题讨论】:

    标签: android xml android-xml


    【解决方案1】:

    您忘记添加 xml 命名空间:

    xmlns:android="http://schemas.android.com/apk/res/android"
    

    试试这个:

    <menu
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <item
            android:id="@+id/item1"
            android:title="Item 1">
        </item><item
            android:id="@+id/item2"
            android:title="Item 1">
        </item>
        <item
            android:id="@+id/item3"
            android:title="Item 1">
        </item>
    </menu>
    

    另请注意,此文件必须存储在 res 目录的 menu 目录中的一个单独的 xml 文件中。

    要在您的活动中使用此菜单,请添加此代码(我希望您的 xml 文件是 main.xml):

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    【讨论】:

    • 我在相对布局中添加了它。 (我的菜单在我的相对布局中,这很糟糕吗?)
    • 该 xml 文件必须在菜单目录中的单独文件中。
    • 哦,我明白了!我如何将菜单与我的活动联系起来?
    • 好的,默认情况下已经有该代码。谢谢,我会尽可能接受。
    【解决方案2】:

    菜单 xml 文件应该在菜单目录下。 这就是找不到类的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多