简体   繁体   English

出现错误:原因:java.lang.NumberFormatException:对于输入字符串:“”

[英]Getting error: Caused by: java.lang.NumberFormatException: For input string: “”

I have two EditText from which I am getting Integer values of 'quantity' and 'rate' and then I am setting this Integer value to TextView names 'Amount'.我有两个EditText ,从中我得到Integer的“数量”和“速率”值,然后我将此Integer值设置为TextView名称“数量”。

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

   
     

I am using TextWatcher on rate whenever the user enter the rate the value will be set to the TextView Amount.每当用户输入速率时,我都会在速率上使用TextWatcher ,该值将设置为TextView数量。 But I am getting NumberFormatException for input string: "".但是我得到了输入字符串的NumberFormatException :“”。

You are not checking the String if it is empty or not before using it.在使用它之前,您没有检查String是否为空。 Since you are beginner I will try to keep it simple for you.由于你是初学者,我会尽量为你保持简单。 Just keep in mind that you need to check for not empty String then you can use it for other purpose.请记住,您需要检查非空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());

The getRate and getQty variables above will be null since edit text will be empty as this code is executed right before the user enters the value.上面的getRategetQty变量将为 null,因为编辑文本将为空,因为此代码在用户输入值之前执行。

Solution:解决方案:

  • From the function, afterTextChanged(Editable s) get the values dynamically from the user through edit text using the code below.从 function 中, afterTextChanged(Editable s)使用以下代码通过编辑文本从用户那里动态获取值。
   s.getText().toString()
  • Add isempty() function to check if the edit text is empty to avoid the null exception.添加 isempty() function 检查编辑文本是否为空,以避免出现 null 异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复由 Caused by: java.lang.NumberFormatException: For input string: "pets" 引起的 Asynctask 错误 - How to fix Asynctask error caused by Caused by: java.lang.NumberFormatException: For input string: "pets" 引起:java.lang.NumberFormatException:空字符串 - Caused by: java.lang.NumberFormatException: empty String 错误 java.lang.NumberFormatException: 对于输入字符串,程序崩溃 - Error java.lang.NumberFormatException: For input string, program crash 我收到此错误:java.lang.NumberFormatException:对于输入字符串:“” - I got this error: java.lang.NumberFormatException: For input string: " " 我在java.lang.NumberFormatException中遇到错误:对于输入字符串:“ null” - I got error in java.lang.NumberFormatException: For input string: “null” 获取错误 java.lang.NumberFormatException: 空字符串 - getting error java.lang.NumberFormatException: empty String java.lang.NumberFormatException: 对于输入字符串:"something" - java.lang.NumberFormatException: For input string:"something" java.lang.NumberFormatException:对于输入字符串:“5.3” - java.lang.NumberFormatException: For input string: "5.3" java.lang.NumberFormatException:对于输入字符串:“1538956627792” - java.lang.NumberFormatException: For input string: "1538956627792" 引起:java.lang.NumberFormatException:对于输入字符串:“androidx.appcompat.widget.AppCompatEditText - Caused by: java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM