Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 当EditText输入为空时,Android应用程序崩溃_Java_Android_Mobile_Crash_Android Edittext - Fatal编程技术网

Java 当EditText输入为空时,Android应用程序崩溃

Java 当EditText输入为空时,Android应用程序崩溃,java,android,mobile,crash,android-edittext,Java,Android,Mobile,Crash,Android Edittext,我试图在EditText为空且按钮被单击时显示祝酒词 单击按钮时,应用程序崩溃 我可能做错了什么?请帮助我,因为我是新的移动应用程序开发。 为了完成这项工作,我需要在哪里进行更改或纠正什么?感谢您的帮助 import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; im

我试图在EditText为空且按钮被单击时显示祝酒词

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

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

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));
        }
    }
}
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入androidx.appcompat.app.appcompat活动;
公共类EmployeeActivity扩展了AppCompatActivity{
EditText hourlyWage、totalRegularHours、totalOvertimeHours;
按钮BTN计算;
文本视图结果;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.employee_活动);
hourlyWage=findViewById(R.id.et_hourlyWage);
totalRegularHours=findViewById(R.id.et_totalRegularHours);
totalOvertimeHours=findViewById(R.id.et_totalOvertimeHours);
btnCalculate=findViewById(R.id.btnCalculate);
结果=(TextView)findViewById(R.id.tv_结果);
}
//要在onClick上调用的方法
公共无效计算结果(视图){
double regularHours=Integer.parseInt(totalRegularHours.getText().toString());
双倍超时;
overtimeHours=Integer.parseInt(totalOvertimeHours.getText().toString());
//检查EditText是否为空
if(hourlyWage.getText().toString().trim().equals(“”){
Toast.makeText(这是“请输入值”,Toast.LENGTH_LONG.show();
}
//检查EditText是否为空
else if(totalRegularHours.getText().toString().trim().equals(“”){
Toast.makeText(这是“请输入值”,Toast.LENGTH_LONG.show();
}
//检查EditText是否超出范围
否则,如果(正常小时数=40){
Toast.makeText(这是“请输入0到40之间的小时值”,Toast.LENGTH_LONG).show();
}
//检查EditText是否超出范围
否则如果(超时小时=30){
Toast.makeText(这是“请输入0到30之间的小时值”,Toast.LENGTH_LONG).show();
}
//计算答案

如果(regularHours>=0&®ularHours您正在使用带有空字符串的
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));
        }


    }
//要在onClick上调用的方法
公共无效计算结果(视图){
//检查EditText是否为空
if(hourlyWage.getText().toString().isEmpty()){
Toast.makeText(这是“请输入值”,Toast.LENGTH_LONG.show();
返回;
}
//检查EditText是否为空
if(totalRegularHours.getText().toString().isEmpty())
{
Toast.makeText(这是“请输入值”,Toast.LENGTH_LONG.show();
返回;
}
int regularhouss=Integer.parseInt(totalregularhouss.getText().toString());
int超时小时;
overtimeHours=Integer.parseInt(totalOvertimeHours.getText().toString());
//检查EditText是否超出范围
如果(正常小时数=40){
Toast.makeText(这是“请输入0到40之间的小时值”,Toast.LENGTH_LONG).show();
}
//检查EditText是否超出范围
否则如果(超时小时=30){
Toast.makeText(这是“请输入0到30之间的小时值”,Toast.LENGTH_LONG).show();
}
//计算答案

如果(regularHours>=0&®ularHours您正在使用带有空字符串的
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));
        }


    }
//要在onClick上调用的方法
公共无效计算结果(视图){
//检查EditText是否为空
if(hourlyWage.getText().toString().isEmpty()){
Toast.makeText(这是“请输入值”,Toast.LENGTH_LONG.show();
返回;
}
//检查EditText是否为空
if(totalRegularHours.getText().toString().isEmpty())
{
Toast.makeText(这是“请输入值”,Toast.LENGTH_LONG.show();
返回;
}
int regularhouss=Integer.parseInt(totalregularhouss.getText().toString());
int超时小时;
overtimeHours=Integer.parseInt(totalOvertimeHours.getText().toString());
//检查EditText是否超出范围
如果(正常小时数=40){
Toast.makeText(这是“请输入0到40之间的小时值”,Toast.LENGTH_LONG).show();
}
//检查EditText是否超出范围
否则如果(超时小时=30){
Toast.makeText(这是“请输入0到30之间的小时值”,Toast.LENGTH_LONG).show();
}
//计算答案

否则,如果(regularHours>=0&®ularHours您可以检查
EditText
是否为空,或者是否使用
TextUtils
类,如下所示:

if(TextUtils.isEmpty(yourEditText.getText().toString())) {
    Toast.makeText(this, "Edit text is empty", Toast.LENGTH_SHORT).show();
    return;
}

您可以使用
TextUtils
类检查
EditText
是否为空,如下所示:

if(TextUtils.isEmpty(yourEditText.getText().toString())) {
    Toast.makeText(this, "Edit text is empty", Toast.LENGTH_SHORT).show();
    return;
}

代码格式代码格式