问题描述
- eclipse 虚拟机无法使用编写的应用程序的menu
-
//按书上做的
//以下为FirstActivity.java
package com.test.activitytest;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;public class FirstActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(FirstActivity.this, "you clicked Button 1", Toast.LENGTH_SHORT).show(); } }); } public boolean onCreateOptionMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.add_item: Toast.makeText(this, "You clicked Add", Toast.LENGTH_LONG).show(); break; case R.id.remove_item: Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show(); break; default: } return true; }
}
//以下为menu/main.xml
<?xml version="1.0" encoding="utf-8"?>android:id="@+id/add_item"
android:title="@string/menu_add"
/>
android:id="@+id/remove_item"
android:title="@string/menu_remove"
/>//以下为 values/string.xml
<string name="app_name">ActivityTest</string> <string name="message">Button 1</string> <string name="menu_add">Add</string> <string name="menu_remove">Remove</string>
写菜单程序只有上面这三个文件
运行了之后没有错误,但是打开虚拟机中的应用程序,然后按menu键没有菜单弹出
初学,求大神指教
解决方案
以上和你原来代码是一样的吧?
很简单,你没有覆盖菜单事件,没加@override啊
时间: 2024-12-21 21:13:05