Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不幸的是,(应用程序)已经停止_Java_Android - Fatal编程技术网

Java 不幸的是,(应用程序)已经停止

Java 不幸的是,(应用程序)已经停止,java,android,Java,Android,我是Java新手,目前我正试图通过修改一些示例,然后创建自己的应用程序来学习Java。 然而,当我尝试启动应用程序时,我一直遇到这个错误。编码站点未触发任何错误。 谁能帮我看看这个,告诉我错误在哪里?提前谢谢。。 下面是我在bmicaculation.java中的代码: package com.example.caloriescalculator; import android.app.Activity; import android.os.Bundle; import android.vie

我是Java新手,目前我正试图通过修改一些示例,然后创建自己的应用程序来学习Java。 然而,当我尝试启动应用程序时,我一直遇到这个错误。编码站点未触发任何错误。 谁能帮我看看这个,告诉我错误在哪里?提前谢谢。。 下面是我在bmicaculation.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";
    }

   }                      
package com.example.caloriescalculator;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.RadioButton;
公共类计算扩展了活动
{
EditText-weightE=(EditText)findViewById(R.id.weightText);
EditText高度=(EditText)findViewById(R.id.heightText);
EditText ageE=(EditText)findViewById(R.id.ageText);
EditText-caloriesresult=(EditText)findViewById(R.id.caloriesText);
RadioButton男性=(RadioButton)findViewById(R.id.maleradio);
RadioButton阴性=(RadioButton)findViewById(R.id.femaleradio);
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmiloayout_main);
按钮卡路里=(按钮)findViewById(R.id.CarriesButton);
setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图arg0)
{    
智力体重=0,年龄=0,身高=0,热量需求=0;
弦的重量、高度、长度;
weightE=getString(R.id.weightText);
heightE=getString(R.id.heightText);
ageE=getString(R.id.ageText);
如果(权重!=“”&&heightE!=“”&&ageE!=“”)
{
if(male.isSelected())
{
热量需求=(智力)(655+9.6*体重+1.8*身高-4.7*年龄);
卡路里结果.setText(卡路里需求);
}
else if(female.isSelected())
{
卡路里需求=(整数)数学圆(66+13.7*体重+5*身高-6.8*年龄);
卡路里结果.setText(卡路里需求);
}
}
}
});
}
public void calculateClickHandler(视图)
{
//确保我们处理计算器按钮的点击
if(view.getId()==R.id.calculateButton)
{           
//获取小部件的引用
EditText-weightText=(EditText)findViewById(R.id.weightText);
EditText heightText=(EditText)findViewById(R.id.heightText);
TextView resultText=(TextView)findViewById(R.id.resultLabel);
//从小部件引用获取用户值
float-weight=float.parseFloat(weightText.getText().toString());
float height=float.parseFloat(heightText.getText().toString());
//计算BMI值
浮动BMI值=calBMI(体重、身高);
//根据BMI值解释类别
字符串bmi解释=解释bmi(bmi值);
//现在在结果文本中设置值
resultText.setText(“您当前的体重指数:“+bmiValue+”-“+BMI解释”);
}
}
//计算体重指数的公式
//查证http://en.wikipedia.org/wiki/Body_mass_index
私人浮标calBMI(浮标重量、浮标高度){
返回(浮动)(重量*10000/(高度*高度));
}  
//解读体重指数的含义
专用字符串解释BMI(浮点BMI值)
{
如果(BMI值<16)
{
返回“严重体重不足”;
}否则如果(BMI值<18.5){
返回“减持”;
}否则如果(BMI值<25){
返回“正常”;
}否则如果(BMI值<30){
返回“超重”;
}否则{
返回“肥胖”;
}
}                      

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

移动

创建后的零件

setContentView(R.layout.bmilayout_main);  

您正在尝试在setContentView之前查找视图。并在已初始化的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);
这不是必需的

您可以使用
weightE
heightText
,这两个属性已经用id
wightText
heightText
初始化。因为它是类成员并且已经初始化,所以无需再次初始化

编辑2:

尽管在onClick中有这样的功能,但将变量重命名为同样名称的editext,以避免混淆

  String weightE, heightE, ageE;
  weightE = getString(R.id.weightText);
  heightE = getString(R.id.heightText); 
要压缩字符串,请使用
.equals
.equalsIgnoreCase

if (!weightE.equals("") && (!heightE.equals("") && (!ageE.equals("")) 

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


你试图学习android而不是java,从你的代码中学习。爪哇!=Android.Android被认为是从developer.Android.com开始学习的,因此你可以从给出的答案中分离出来,Java(因此,Android)不能使用
==
来比较
字符串=(这是可能的,但肯定不是您所期望的)。使用
equals()
代替。感谢您的输入..是的,我正在用Java学习Android。非常感谢。这有帮助:)。我现在可以启动并进入应用程序后
if (!weightE.equals("") && (!heightE.equals("") && (!ageE.equals("")) 
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