Getting a value of int from edittext

只愿长相守 提交于 2019-12-14 03:30:18

问题


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.



来源:https://stackoverflow.com/questions/45650927/getting-a-value-of-int-from-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!