您好,我学习在 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);
}
}
}
有什么解决办法吗?谢谢 :)