本文介绍了从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); }

希望它对您有用.

这篇关于从edittext获取int的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 20:14