我是 Java 新手,目前我正在尝试通过修改一些示例然后创建自己的应用程序来学习 Java。但是,当我尝试启动应用程序时,我不断遇到此错误。编码站点没有触发错误。谁能帮忙看看这个并告诉我错误在哪里?在此先感谢.. 以下是我在 BMIcalculation.java 中的代码:
package com.example.caloriescalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
public class BMIcalculation extends Activity
{
EditText weightE = (EditText)findViewById(R.id.weightText);
EditText heightE = (EditText)findViewById(R.id.heightText);
EditText ageE = (EditText)findViewById(R.id.ageText);
EditText caloriesresult = (EditText)findViewById(R.id.caloriesText);
RadioButton male = (RadioButton) findViewById(R.id.maleradio);
RadioButton female = (RadioButton) findViewById(R.id.femaleradio);
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmilayout_main);
Button calories = (Button) findViewById(R.id.caloriesButton);
calories.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
int weight = 0, age = 0, height = 0, caloriesneed = 0;
String weightE, heightE, ageE;
weightE = getString(R.id.weightText);
heightE = getString(R.id.heightText);
ageE = getString(R.id.ageText);
if (weightE != "" && heightE != "" && ageE != "")
{
if (male.isSelected())
{
caloriesneed = (int) (655 + 9.6*weight + 1.8*height - 4.7*age);
caloriesresult.setText(caloriesneed);
}
else if (female.isSelected())
{
caloriesneed = (int) Math.round (66 + 13.7*weight + 5*height - 6.8*age);
caloriesresult.setText(caloriesneed);
}
}
}
});
}
public void calculateClickHandler(View view)
{
// make sure we handle the click of the calculator button
if (view.getId() == R.id.calculateButton)
{
// get the references to the widgets
EditText weightText = (EditText)findViewById(R.id.weightText);
EditText heightText = (EditText)findViewById(R.id.heightText);
TextView resultText = (TextView)findViewById(R.id.resultLabel);
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the BMI value
float bmiValue = calBMI(weight, height);
// interpret the category based on the BMI value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText("Your current BMI :" + bmiValue + " - " + bmiInterpretation);
}
}
// the formula to calculate the BMI index
// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calBMI (float weight, float height) {
return (float) (weight *10000 / (height * height));
}
// interpret what BMI means
private String interpretBMI(float bmiValue)
{
if (bmiValue < 16)
{
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}