新手请教一个错误问题!
下文的代码是参照一本叫《深入浅出_Google_Android》做的实例。
在编译过程中出现了问题,由于本人初学,找遍了资料也没弄明白问题出在哪里,希望大家帮个忙,感激不尽!
源码:
package mm.demo.bmi;
import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class BMI extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_bmi, menu);
return true;
//Listen for button clicks
Button button=(Button)findViewById(R.id.submit);
Button.setonclicklistener(calcBMI);这一句的“setonclicklistener”提示错误。
}
private OnClickListener calcBMI=new OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf=new DecimalFormat("0.00");
EditText fieldheight=(EditText)findViewById(R.id.height);
EditText fieldweight=(EditText)findViewById(R.id.weight);
double height=Double.parseDouble(fieldheight.getText().toString())/100;
double weight=Double.parseDouble(fieldweight.getText().toString());
double BMI=weight/(height*height);
TextView result=(TextView)findViewById(R.id.result);
result.setText("Your BMI is "+nf.format(BMI));
//Give health advice
TextView fieldsuggest=(TextView)findViewById(R.id.suggest);
if(BMI>25){
fieldsuggest.setText(R.string.jyheavy);
}else if(BMI<20){
fieldsuggest.setText(R.string.jylight);
}else{
fieldsuggest.setText(R.string.jyaverage);
}
}
};
}
//Listen for button clicks
PS错误提示:
The method setOnclicklistener(View.OnClickListener) is undefined for the type Button。
谢谢了!
------解决方案--------------------
大小写setonclicklistener注意
------解决方案--------------------
setOnclicklistener 应该是 setOnClickListener
------解决方案--------------------
呵呵,是这个错误。