activity.java
package your.mypackage.namespace;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class WangActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreatOptionMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0,0,0,R.string.app_about);
menu.add(0,1,0,R.string.str_exit);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case 0:
openOptionDialog();
break;
case 1:
finish();
break;
}
return true;
}
private void openOptionDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
}
)
.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@+drawable/white"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
------解决方案--------------------
怎么没人回呢,我来。。
注意这个onCreatOptionMenu方法,有拼写错误哦,应该是onCreateOptionsMenu,应该是重写Activity这个方法,详细原因你懂得。
在Eclipse重写方法最好使用快捷键,即快,又不容易出错。(1.选中要重写的类,2.按shift+alt+s,3.选择重写/覆盖方法,4.选择要重写的方法就可以了)