从edittext获取int的值

问题描述:

我试图从edittext中获取一个值,然后将其强制转换为int,以便我可以为减法/additon/等等处理该值.这是我使用的代码,但是使用此代码会导致崩溃.

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

请帮助我,非常感谢

我认为,当您运行程序时,编辑文本没有值,这意味着它将返回NULL,并且NULL不能为Integer值,因此您必须检查条件类似

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

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

希望它对您有用.