gpt4 book ai didi

java - 当 EditText 输入为空时 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 04:54:28 25 4
gpt4 key购买 nike

当 EditText 为空且单击按钮时,我试图显示 toast 。

单击按钮时应用程序崩溃。

我可能做错了什么?请帮助我解决这个问题,因为我是移动应用程序开发的新手。为了完成这项工作,我需要在哪里进行更改或纠正什么?感谢您的帮助。

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class EmployeeActivity extends AppCompatActivity {

EditText hourlyWage, totalRegularHours, totalOvertimeHours;
Button btnCalculate;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.employee_activity);
hourlyWage = findViewById(R.id.et_hourlyWage);
totalRegularHours = findViewById(R.id.et_totalRegularHours);
totalOvertimeHours = findViewById(R.id.et_totalOvertimeHours);
btnCalculate = findViewById(R.id.btnCalculate);
result = (TextView) findViewById(R.id.tv_Result);
}

// method to be called on onClick
public void calculateResult(View view) {
double regularHours = Integer.parseInt(totalRegularHours.getText().toString());
double overtimeHours;
overtimeHours = Integer.parseInt(totalOvertimeHours.getText().toString());
// check if EditText is empty
if (hourlyWage.getText().toString().trim().equals("")) {
Toast.makeText(this, "Please Enter The Value.", Toast.LENGTH_LONG).show();
}
// check if EditText is empty
else if (totalRegularHours.getText().toString().trim().equals("")) {
Toast.makeText(this, "Please Enter The Value.", Toast.LENGTH_LONG).show();
}
// check if EditText is out of range
else if (regularHours <= 0 && regularHours >= 40) {
Toast.makeText(this, "Please Enter The Value of Hours Between 0 to 40.", Toast.LENGTH_LONG).show();
}
// check if EditText is out of range
else if (overtimeHours <= 0 && overtimeHours >= 30) {
Toast.makeText(this, "Please Enter The Value of Hours Between 0 to 30.", Toast.LENGTH_LONG).show();
}
// calculates the answer
else if (regularHours >= 0 && regularHours <= 40) {
// formula for calculation
double regularWage;
regularWage = (Integer.parseInt(hourlyWage.getText().toString()) * Integer.parseInt(totalRegularHours.getText().toString())) + (Integer.parseInt(totalOvertimeHours.getText().toString()) * Integer.parseInt(hourlyWage.getText().toString()) * (1.5));
//displays the result in a TextView
result.setText(String.valueOf(regularWage));
}
}
}

最佳答案

您正在使用带有空字符串的 Integer.parseInt 进行解析。此外,您正在以不同的类型存储整数 - double 。这是您的解决方法:

// method to be called on onClick
public void calculateResult(View view) {

// check if EditText is empty
if (hourlyWage.getText().toString().isEmpty()){
Toast.makeText(this, "Please Enter The Value.", Toast.LENGTH_LONG).show();
return;
}
// check if EditText is empty
if(totalRegularHours.getText().toString().isEmpty())
{
Toast.makeText(this, "Please Enter The Value.", Toast.LENGTH_LONG).show();
return;
}

int regularHours = Integer.parseInt(totalRegularHours.getText().toString());

int overtimeHours;
overtimeHours = Integer.parseInt(totalOvertimeHours.getText().toString());

// check if EditText is out of range
if (regularHours <= 0 && regularHours >= 40) {

Toast.makeText(this, "Please Enter The Value of Hours Between 0 to 40.", Toast.LENGTH_LONG).show();

}
// check if EditText is out of range
else if (overtimeHours <= 0 && overtimeHours >= 30) {

Toast.makeText(this, "Please Enter The Value of Hours Between 0 to 30.", Toast.LENGTH_LONG).show();

}
// calculates the answer
else if(regularHours >= 0 && regularHours <= 40) {
// formula for calculation
double regularWage;
regularWage = (Integer.parseInt(hourlyWage.getText().toString()) * Integer.parseInt(totalRegularHours.getText().toString())) + (Integer.parseInt(totalOvertimeHours.getText().toString()) * Integer.parseInt(hourlyWage.getText().toString()) * (1.5));
//displays the result in a TextView
result.setText(String.valueOf(regularWage));
}


}

关于java - 当 EditText 输入为空时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59473996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com