如何在edittext中输入任何内容时避免异常?

Spi*_*ike 1 android exception android-edittext

我创建了edittexts,用户可以在其中输入数字.在我的应用程序,用户可以通过点击提交按钮提交值.(注:最初我将所有3个EditTexts为""用的setText()中的代码)提交我一直以下这些线路中的方法来检索值后.

String st1=editText1.getText().toString();
int tempVal1=Integer.parseInt(st);
String st2=editText2.getText().toString();
int tempVa2=Integer.parseInt(st);
String st3=editText2.getText().toString();
int tempVal3=Integer.parseInt(st);
Run Code Online (Sandbox Code Playgroud)

但在这里我的问题是,如果用户没有在第一EDITTEXT输入任何值和填充只有第二和第三并提交,然后在tempVal1的值应为0,但我得到异常监守,因为不是第一次声明有任何数据.如何避免此异常并在未填写相应edittext中的任何内容时保持tempVal 0?

我有一个疑问,当我添加这一 int x=Integer.valueOf(editText.getText());行时,我收到以下错误.为什么?The method valueOf(String) in the type Integer is not applicable for the arguments (Editable)

请澄清我的疑虑.

Luc*_*fer 7

EditText在将其转换为Number之前,您需要检查它.

if( !editText1.getText().toString().equals("") && editText1.getText().toString().length() > 0 )
{
    // Get String
    Integer.parseInt(editText1.getText().toString());
}
Run Code Online (Sandbox Code Playgroud)