Getting a value of int from edittext

2019-09-21 22:39发布

问题:

I am trying to get a value from the edittext then cast it to int so that I can process the value for a substraction/additon/and so on. Here is the code I use, but I keep getting crash using this code.

number = (EditText) findViewById(R.id.editText);
angka = Integer.parseInt(number.getText().toString());
//my alternative code is like this : angka = Integer.parseInt(String.Valueof(number));

Please I need your help, thanks you so much

回答1:

I think when you run your program then edit text have no value that means it will return NULL and NULL can't be a Integer value so you must have to check Condition Like

String editTextValue = number.getText().toString(); if(!TextUtils.isEmpty(editTextValue)){ angka = Integer.parseInt(editTextValue); }

I hope its work for you.