编辑菜单时遇到bug...请教各位...谢谢~
源码如下
package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
public class Bmi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
}
protected static final int MENU_ABOUT=Menu.FIRST;
protected static final int MENU_QUIT=Menu.FIRST+1;
@Override
public boolean onCreatOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreatOptionsMenu(menu);
menu.add(0, MENU_ABOUT, 0, "About...")
.setIcon(android.R.drawable.ic_menu_help);
menu.add(0, MENU_QUIT, 0, "EXIT")
.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case MENU_ABOUT:
openOptionsDialog();
break;
case MENU_QUIT:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
private void openOptionsDialog() {
// TODO Auto-generated method stub
new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.show();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;
private void findViews()
{
button_calc= (Button)findViewById(R.id.submit);
field_height=(EditText)findViewById(R.id.height);
field_weight=(EditText)findViewById(R.id.weight);
view_result=(TextView)findViewById(R.id.result);
view_suggest=(TextView)findViewById(R.id.suggest);
}
private void setListensers()
{
button_calc.setOnClickListener(calcBMI);
}
private Button.OnClickListener calcBMI = new Button.OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf= new DecimalFormat("0.00");
double height=Double.parseDouble(field_height.getText().toString())/100;
double weight=Double.parseDouble(field_weight.getText().toString());
double BMI=weight/(height*height);
view_suggest.setText(getText(R.string.bmi_result)+nf.format(BMI));
if(BMI>25)
{
view_suggest.setText(R.string.advice_heavy);
}
else if(BMI<25)
{
view_suggest.setText(R.string.advice_light);
}else
{
view_suggest.setText(R.string.advice_average);
}
}
标出的红色行 为Eclipse的报错,实在不知道错在哪里...麻烦大家看下,很感谢!
[解决办法]
public boolean onCreatOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreatOptionsMenu(menu);
onCreatOptionsMenu(Menu menu); 拼写错误, onCreateOptionsMenu
[解决办法]
我也觉得是creat忘记e了。