Getting error: Caused by: java.lang.NumberFormatException: For input string: ""

Getting error: Caused by: java.lang.NumberFormatException: For input string: ""

我有两个 EditText,我从中得到 'quantity' 和 'rate' 的 Integer 值,然后我将这个 Integer 值设置为 TextView 姓名 'Amount'.

    private MilkViewModel milkViewModel;
    private DatePickerDialog datePickerDialog;
    private Button dateButton,submit;
    private EditText dateText;
    private MaterialDatePicker materialDatePicker;
    private Spinner shift_spinner;
    private ImageView imageView;
   public RadioGroup rg;

    private EditText fat;
    private EditText snf;
    public EditText rate;
    public EditText qty;
    private TextView amount;

    private RadioButton rb;
    private String getDate,getShift,getType;
    private int getSnf=0;
    private int getRate;
    private int getQty;


 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dateText=findViewById(R.id.date_picker_et);
        shift_spinner = (Spinner) findViewById(R.id.shift_spinner);
        rg=(RadioGroup)findViewById(R.id.radioGroup);

        snf=(EditText)findViewById(R.id.snf);
        rate=(EditText)findViewById(R.id.rate);
        fat=(EditText)findViewById(R.id.fat);
        amount= (TextView) findViewById(R.id.amount);
        final TextView amount = findViewById(R.id.amount);

        qty=(EditText)findViewById(R.id.qty);
        submit=(Button)findViewById(R.id.submit);


       // int getAmount = Integer.parseInt(amount.getText().toString());
        int getRate=Integer.parseInt(rate.getText().toString());
        int getQty=Integer.parseInt(qty.getText().toString());


        rate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                amount.setText(Integer.toString(getRate*getQty));
                Toast.makeText(MainActivity.this, ""+amount, Toast.LENGTH_SHORT).show();
            }
        });

   
     

我在费率上使用 TextWatcher 每当用户输入费率时,该值将设置为 TextView 金额。但是我得到 NumberFormatException 输入字符串:“”。

您在使用前没有检查 String 是否为空。由于你是初学者,我会尽量为你保持简单。请记住,您需要检查是否为空 String 然后您可以将其用于其他目的。

rate.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String tempRate = rate.getText().toString();
        String tempQty = qty.getText().toString();

        if (tempRate != null && !tempRate.isEmpty() && !tempRate.equals("null") && tempQty != null && !tempQty.isEmpty() && !tempQty.equals("null")) {
            int getRate=Integer.parseInt(rate.getText().toString());
            int getQty=Integer.parseInt(qty.getText().toString());
        }
        amount.setText(Integer.toString(getRate*getQty));
        Toast.makeText(MainActivity.this, ""+amount, Toast.LENGTH_SHORT).show();
    }
});
   int getRate=Integer.parseInt(rate.getText().toString());
   int getQty=Integer.parseInt(rate.getText().toString());

getRategetQty变量上面将为空,因为编辑文本将为空,因为此代码是在用户输入值之前立即执行的。

解法:

  • 从函数中,afterTextChanged(Editable s) 使用以下代码通过编辑文本从用户动态获取值。
   s.getText().toString()
  • 添加 isempty() 函数来检查编辑文本是否为空以避免 null 异常。