0

您好,我学习在 Android 中使用菜单。我设置了最小 SDK = 8 和最大 SDK = 17。我希望当应用程序在最小 SDK 中运行时,菜单显示在底部。并在应用程序栏顶部的最新 SDK 显示菜单中。

但这是错误的:

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

这是我的menu.xml文件

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

这是我的MainActivity.java文件

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);
        }
    }
}

有什么解决办法吗?谢谢 :)

4

2 回答 2

2

改变这个

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

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

你也有一个布局,比如menu.xmlif so 重命名它以避免混淆activity_main.xml

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

您可以查看文档。有代码片段

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

于 2013-11-01T19:09:16.087 回答
1

在最后一个菜单项中更改android:text="Delete"为。android:title="Delete"

于 2013-11-01T19:09:08.460 回答