user1834464
user1834464

Reputation:

Android | The following classes could not be found: menu and item

I'm trying to put a menu on my application, but when I put a menu tag in my xml like this:

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

I get this error message when I go on the graphical layout.

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)

Anyone know why?

Upvotes: 3

Views: 7164

Answers (2)

Cap Barracudas
Cap Barracudas

Reputation: 2505

The menu xml files should be under the menu directory. That why the classes cannot be found.

Upvotes: 0

rekire
rekire

Reputation: 47945

You forgot to add the xml namespace:

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

Try this:

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

Please note also that this file has to be stored in a seperat xml file in the menu directory in your res directory.

For using this menu in your activity add this code (I expect that your xml file is 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;
}

Upvotes: 5

Related Questions