簡體   English   中英

當 EditText 輸入為空時,Android 應用程序崩潰

[英]Android Application Crashes When EditText Is Entered Empty

當 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));
        }
    }
}

您正在使用帶有空字符串的Integer.parseInt進行解析。 此外,您正在以不同的類型存儲整數 - 雙精度。 這是你的修復:

// 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));
        }


    }

您可以使用TextUtils類檢查EditText是否為空,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM