-1

我是 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";
    }

   }                      

}

4

3 回答 3

1

如果没有 logcat 错误,我只能告诉你:

移动

weightE = (EditText)findViewById(R.id.weightText);
heightE = (EditText)findViewById(R.id.heightText);
ageE = (EditText)findViewById(R.id.ageText);
caloriesresult = (EditText)findViewById(R.id.caloriesText);
male = (RadioButton) findViewById(R.id.maleradio); 
female = (RadioButton) findViewById(R.id.femaleradio);

部分到onCreate之后

setContentView(R.layout.bmilayout_main);  

您正在尝试在 setContentView 之前查找视图。并在您已初始化的 onCreate 之外声明。纠正那个。希望这可以帮助。

于 2013-11-01T04:23:01.873 回答
1

将所有初始化移到里面onCreate

EditText weightE ;
EditText heightE ;
EditText ageE ;
EditText caloriesresult;
RadioButton male ;
RadioButton female; 
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState); 
setContentView(R.layout.bmilayout_main); // must be first 
weightE = (EditText)findViewById(R.id.weightText); // then initialize your views
heightE = (EditText)findViewById(R.id.heightText);
ageE = (EditText)findViewById(R.id.ageText);
caloriesresult = (EditText)findViewById(R.id.caloriesText);
male = (RadioButton) findViewById(R.id.maleradio); 
female = (RadioButton) findViewById(R.id.femaleradio);
...// rest of the code

findViewById查找具有当前膨胀布局中提到的 id 的视图。所以你需要先设置布局的内容并初始化你的视图。如果没有,您将获得 NPE

编辑:

你也有

 EditText weightText = (EditText)findViewById(R.id.weightText);
 EditText heightText = (EditText)findViewById(R.id.heightText);

这不是必需的

您可以使用weightEandheightE 已经用 idwightTextheightText. 由于它是类成员并且已经初始化,因此无需再次初始化

编辑2:

尽管您在 onClick 中有此内容,但仍将变量重命名为具有相同名称的编辑文本以避免混淆

  String weightE, heightE, ageE;
  weightE = getString(R.id.weightText);
  heightE = getString(R.id.heightText); 

并且要比较字符串,请使用.equalsor .equalsIgnoreCase

if (!weightE.equals("") && (!heightE.equals("") && (!ageE.equals("")) 
于 2013-11-01T04:23:09.343 回答
1

在设置内容视图之前不能初始化组件OnCreate()。所以只需在 onCreate() 之前声明它并在 OnCreate() 中设置内容视图后初始化所有组件。

package com.example.caloriescalculator;


public class BMIcalculation extends Activity
{
EditText weightE;
EditText heightE ;
EditText ageE ;
EditText caloriesresult ;
RadioButton male; 
RadioButton female;

Button calories;

 EditText weightText ;
 EditText heightText ;
 TextView resultText ;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bmilayout_main);   

 weightE = (EditText)findViewById(R.id.weightText);
 heightE = (EditText)findViewById(R.id.heightText);
 ageE = (EditText)findViewById(R.id.ageText);
 caloriesresult = (EditText)findViewById(R.id.caloriesText);
 male = (RadioButton) findViewById(R.id.maleradio); 
 female = (RadioButton) findViewById(R.id.femaleradio);  
 calories = (Button) findViewById(R.id.caloriesButton); 


    calories.setOnClickListener(new OnClickListener() 
       {
         @Override
                public void onClick(View arg0) 
                {    
                    // rest of your code
于 2013-11-01T04:23:29.593 回答