【问题标题】:App crashes after button clicked with empty edittext使用空编辑文本单击按钮后应用程序崩溃
【发布时间】:2014-07-21 23:31:01
【问题描述】:

我正在开发 BMI 计算器应用程序。它有两个editText、一个按钮和三个textView。如果单击该按钮,您将获得 BMI,但当一个或两个 edittext 字段为空时,应用程序会崩溃。我在我的代码中看不到任何错误。你能帮我一个人吗? 非常感谢

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculator);
}

public void calculateClickHandler(View view) {

    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;
        if ("".equals(weightText.getText())) {
            weight = 1;
        } else {
            weight = Float.parseFloat(weightText.getText().toString());
        }

        float height;
        if ("".equals(heightText.getText())) {
            height = 1;
        } else {
            height = Float.parseFloat(heightText.getText().toString());
        }

        float bmiValue = calculateBMI(weight, height);
        String bmiInterpretation = interpretBMI(bmiValue);
        resultText.setText(bmiValue + "-" + bmiInterpretation);
    }
}

private float calculateBMI (float weight, float height) {
    return (float) (weight / ((height / 100) * (height / 100)));
}

private String interpretBMI(float bmiValue) {
    if (bmiValue < 16) {
        return "Severely underweight";
    } else if (bmiValue >=16 && bmiValue < 18.5) {
        return "Underweight";
    } else if (bmiValue >= 18.5 && bmiValue < 25) {
        return "Normal";
    } else if (bmiValue >= 25 && bmiValue < 30) {
        return "Overweight";
    } else if (bmiValue >= 30) {
        return "Obese";
    } else {
        return "Incorrect BMI";
    }
}

【问题讨论】:

  • 总是如果您遇到异常,请在您的问题中包含 logcat。
  • 您的if 语句应该使用toString() 方法...if ("".equals(weightText.getText().toString()))try/catch 会更好(更简单?)。是的,正如 Xaver 所说的那样。
  • 我在你的代码中看不到按钮...
  • @AlexBalo 可能在 xml 中设置 public void calculateClickHandler(View view)
  • 一些改进:这是错误的"".equals(heightText.getText()) 而不是这样做heightText.getText().toString().isEmpty()。这个转换为float 是多余的:return (float) (weight / ((height / 100) * (height / 100)));,你不需要在这里只使用float。并且您应该全局保存 EditTexts 的实例。

标签: android crash android-edittext


【解决方案1】:

您的应用崩溃的原因是这些if 语句:

if ("".equals(weightText.getText())) {
    weight = 1;
}

weightText.getText() 不返回 String,而是返回 Editable。另外Strings 有一个名为isEmpty() 的方法来检查它们是否为空。所以用这样的东西替换你的ifs:

if(weightText.getText().toString().isEmpty()) {
    ...
}

希望对您有帮助,如果您有任何其他问题,请随时提问!

【讨论】:

    【解决方案2】:

    试试这个:heightText.getText().toString()

    【讨论】:

      【解决方案3】:

      用“”初始化您的 EditText。 例如:

      <EditText
          android:id="@+id/weightText"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text=""
       />
      

      改变

      weightText.getText()
      

      weightText.getText().toString();
      

      【讨论】:

        【解决方案4】:

        根据我在您的帖子中看到的内容,您可以尝试:

        float weight;
        if (weightText.getText() == null || "".equals(weightText.getText().toString())) {
            weight = 1;
        } else {
            weight = Float.parseFloat(weightText.getText().toString());
        }
        
        float height;
        if (heightText.getText() == null || "".equals(heightText.getText().toString())) {
            height = 1;
        } else {
            height = Float.parseFloat(heightText.getText().toString());
        }
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-01
          • 2017-02-14
          • 1970-01-01
          • 1970-01-01
          • 2021-02-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多