Android空editText正在崩溃我的程序

时间:2017-01-27 17:18:32

标签: java android

第一次使用Java,我正在尝试为我工作的餐厅的同事创建一个简单的提示计算器,但是当我将其中一个editText字段留空时程序崩溃了。

MainACtivity:

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

    totalTipsInput = (EditText) findViewById(R.id.totalTipsInput);
    waiter1Hours = (EditText) findViewById(R.id.waiter1Hours);
    waiter2Hours = (EditText) findViewById(R.id.waiter2Hours);
    waiter3Hours = (EditText) findViewById(R.id.waiter3Hours);
    waiter4Hours = (EditText) findViewById(R.id.waiter4Hours);

    tipsPerHourView = (TextView) findViewById(R.id.tipsPerHourView);
    totalHoursView = (TextView) findViewById(R.id.totalHoursView);
    barsCutView = (TextView) findViewById(R.id.barsCutView);

    waiter1Pay = (TextView) findViewById(R.id.waiter1Pay);
    waiter2Pay = (TextView) findViewById(R.id.waiter2Pay);
    waiter3Pay = (TextView) findViewById(R.id.waiter3Pay);
    waiter4Pay = (TextView) findViewById(R.id.waiter4Pay);
    taxDepositView = (TextView) findViewById(R.id.taxDepositView);

    Button calcBtn = (Button) findViewById(R.id.calcBtn);
    calcBtn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {

            double totalTips = Double.parseDouble(totalTipsInput.getText().toString());

            double cWaiter1Hours = Double.parseDouble(waiter1Hours.getText().toString());
            double cWaiter2Hours = Double.parseDouble(waiter2Hours.getText().toString());
            double cWaiter3Hours = Double.parseDouble(waiter3Hours.getText().toString());
            double cWaiter4Hours = Double.parseDouble(waiter4Hours.getText().toString());

            double resultTotalHours = cWaiter1Hours + cWaiter2Hours + cWaiter3Hours + cWaiter4Hours;
            double resultBarsCut = (totalTips * 7) / 100;
            double resultTaxDeposit = resultTotalHours * 3;
            double resultTipsPerHour = (totalTips - resultBarsCut - resultTaxDeposit) / resultTotalHours;

            double resultWaiter1Pay = cWaiter1Hours * resultTipsPerHour;
            double resultWaiter2Pay = cWaiter2Hours * resultTipsPerHour;
            double resultWaiter3Pay = cWaiter3Hours * resultTipsPerHour;
            double resultWaiter4Pay = cWaiter4Hours * resultTipsPerHour;

            totalHoursView.setText(Double.toString(resultTotalHours));
            tipsPerHourView.setText(Double.toString(resultTipsPerHour));
            barsCutView.setText(Double.toString(resultBarsCut));

            waiter1Pay.setText(Double.toString(resultWaiter1Pay));
            waiter2Pay.setText(Double.toString(resultWaiter2Pay));
            waiter3Pay.setText(Double.toString(resultWaiter3Pay));
            waiter4Pay.setText(Double.toString(resultWaiter4Pay));

            taxDepositView.setText(Double.toString(resultTaxDeposit));


        }
    });
}

试图做这样的事情,但是.length()出错:

                if (double totalTips = Double.parseDouble(totalTipsInput.getText().toString()).length() < 1 || totalTipsInput = null) {
                totalTips = 0
            } else {
                double totalTips = Double.parseDouble(totalTipsInput.getText().toString());
            }

2 个答案:

答案 0 :(得分:1)

在班上使用此方法:

 public static Double returnDouble(EditText editText)
    {
        try {
            if(editText.getText().toString().isEmpty())
            {
                return 0d;
            }
            else
            {
                return Double.parseDouble(editText.getText().toString());
            }
        } catch (NumberFormatException e) {

            return 0d;
        }

    }

答案 1 :(得分:0)

您可以执行以下操作以确保“parseDouble”的输入有效:

    double totalTips = 0;
    Editable totalString = totalTipsInput.getText();
    if(totalString.length() > 0){
        totalTips = Double.parseDouble(totalString.toString());
    }

如果“totalTips”字段可以接受用户输入,则需要确保它们只能输入有效数字。懒惰的方式可能是在parseDouble周围放置一个try / catch,并处理有人可能输入无法解析为double的内容的情况(即空字符串,字母,格式错误的数值)

我建议不要相信用户也要在其余的服务员小时字段中输入有效值。在尝试解析输入之前,您可能希望对这些字段执行类似的检查。