fanjavaid
fanjavaid

Reputation: 1738

Error inflating menu in Android

Hello i learn to use menu in Android. And i set minimum SDK = 8 and Maximum SDK = 17. I want when the Application run in minimum SDK the menu show at the bottom. And in latest SDK show menu on top of application bar.

But it was error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidmenu/com.example.androidmenu.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class menu

Here is my menu.xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/menu_settings"
        android:icon="@drawable/icon_settings"
        android:title="Settings"/>

    <item
        android:id="@+id/menu_save"
        android:icon="@drawable/icon_save"
        android:title="Save"/>

    <item
        android:id="@+id/menu_search"
        android:icon="@drawable/icon_search"
        android:title="Search"/>

    <item
        android:id="@+id/menu_delete"
        android:icon="@drawable/icon_delete"
        android:text="Delete"/>

</menu>

And here is my MainActivity.java file

package com.example.androidmenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
    }


    @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);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }

    // Event Handling for individual menu
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch(menuItem.getItemId()) {
            case R.id.menu_settings:
                Toast.makeText(MainActivity.this, "Settings selected", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_save:
                Toast.makeText(MainActivity.this, "Save selected", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_search:
                Toast.makeText(MainActivity.this, "Search selected:", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_delete:
                Toast.makeText(MainActivity.this, "Delete selected", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }
}

Any solutions? Thank you :)

Upvotes: 0

Views: 7517

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

change this

menuInflater.inflate(R.layout.menu, menu);

To

menuInflater.inflate(R.menu.menu, menu); // assuming menu.xml in /res/menu/menu.xml

Also do you have a layout like menu.xml if so rename it to avoid confusion to activity_main.xml

setContentView(R.layout.activity_main); // res/layout/activity_main.xml

You can check the docs. has code snippets

http://developer.android.com/guide/topics/ui/menus.html

Upvotes: 2

kiruwka
kiruwka

Reputation: 9450

Change android:text="Delete" to android:title="Delete" in your last menu item.

Upvotes: 1

Related Questions