Search code examples
androidandroid-layoutandroid-xmlandroid-relativelayoutandroid-menu

java.lang.RuntimeException: Expecting menu, got RelativeLayout error when clicking menu button


I am creating a MapView and wanted to add a Menu when you click on the Menu Button on your Phone. So I did that and it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/my_location"
          android:icon="@drawable/compass_base"
          android:title="Meine Position"
          android:showAsAction="ifRoom"/>
</menu>

And my activity looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <com.google.android.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="0zPcuLDMKtZhz-000000000000000000"
    />

    <TextView
        android:id="@+id/tvLongitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/defaultLongitude" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Latidude:" />

    <TextView
        android:id="@+id/tvLatitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/defaultLatitude" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="17dp"
        android:layout_toLeftOf="@+id/tvLongitude"
        android:text="Longitude" />

</RelativeLayout>

Now the Problem is when I click the Menu Button it just closes and Logcat says this:

10-24 07:26:20.736: E/AndroidRuntime(372): java.lang.RuntimeException: Expecting menu, got RelativeLayout

I want to leave the RelativeLayout because I can make things show on the MapView like I am doing now the Longitude and Latidude

This is the MainActivity Method for the Menu:

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

Solution

  • Android provides developers guide for menus.
    Based on it, You should do inflation of the menu the following way:

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

    So, menuInflater.inflate() first parameter in Yours code is incorrect. You should use menu xml which is located in your res/menu folder.