你好,我學會了在Android中使用菜單。並且我設置了最小SDK = 8和最大SDK = 17。 我希望當應用程序在最小SDK中運行時,菜單顯示在底部。並在應用程序欄頂部的最新SDK顯示菜單中。Android中的錯誤充氣菜單
但它是錯誤:
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);
}
}
}
任何解決方案?謝謝:)
你有名稱菜單的佈局嗎? – Raghunandan
它應該是'R.menu.menu',如果它在'menu'文件夾 – codeMagic
ooh,我想我的menu.xml也是我的佈局。我現在將'R.layout.menu'改爲'R.layout.activity_main'。 :D 但該圖標不會顯示在最新的SDK中。 – fanjavaid