尝试为数字创建验证,尝试进行测试时崩溃

时间:2018-10-31 21:32:47

标签: java android

标题是我能解释的最好的。我正在尝试使用android studio在Java中为数字框创建验证。但是,当验证程序改为显示消息“必须输入重量!”时,程序会自行崩溃。知道有什么问题吗?预先感谢。

package com.example.student.project8a_medicalcalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.util.EmptyStackException;

public class MainActivity extends AppCompatActivity {
double weightEntered;
double convertedWeight;
final double conversionRate = 2.2;


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


    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    final EditText weight = (EditText) findViewById(R.id.Weight);
    final RadioButton lbToKilo = (RadioButton) findViewById(R.id.LbToKilo);
    final RadioButton kiloToLB = (RadioButton) findViewById(R.id.KiloToLb);
    final TextView result = (TextView) findViewById(R.id.Results);
    Button convert = (Button) findViewById(R.id.bnConvert);

    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String weightV;
            weightV = weight.getText().toString();
            if (weightV.equals(" "))Toast.makeText(MainActivity.this, "Weight must be entered! ", Toast.LENGTH_LONG).show();

            weightEntered = Double.parseDouble(weight.getText().toString());
            DecimalFormat tenth = new DecimalFormat("#.#");

            if (lbToKilo.isChecked()){
                if (weightEntered <= 500){
                    convertedWeight = weightEntered / conversionRate;
                    result.setText(tenth.format(convertedWeight) + " kilograms");
                } else {
                    Toast.makeText(MainActivity.this, "Pounds must be less than 500", Toast.LENGTH_LONG).show();
                }

            }
            if (kiloToLB.isChecked()){
                if (weightEntered <= 225){
                    convertedWeight = weightEntered * conversionRate;
                    result.setText(tenth.format(convertedWeight) + " pounds");}
                    else {
                    Toast.makeText(MainActivity.this, "Kilos must be less than 225 ", Toast.LENGTH_LONG).show();

                }
            }
        }
    });

}

}

2 个答案:

答案 0 :(得分:0)

Double.parseDouble()将引发NumberFormatException异常,并在您尝试解析空字符串时使应用程序崩溃。

您检查了输入字符串,但即使它等于空也运行Double.parseDouble()

您可以执行以下操作:

if (!weightV.isEmpty()){
    weightEntered = Double.parseDouble(weight.getText().toString());
    ......
}else{
    Toast.makeText(MainActivity.this, "Weight must be entered! ", Toast.LENGTH_LONG).show();
    ......
}

答案 1 :(得分:0)

您说:验证,当它改为显示消息“必须输入重量!”时,/程序自动崩溃
因此,您在EditText中放置了一个空格" ",并希望看到Toast。
如果更改代码,您将看到Toast:

if (weightV.trim().isEmpty()) {
    Toast.makeText(MainActivity.this, "Weight must be entered! ", Toast.LENGTH_LONG).show();
    return;
}

但是如果没有return,代码将继续执行,并且Double.parseDouble(weight.getText().toString())会引发异常,因为空的EditText文本不是有效数字。