空的EditText导致程序崩溃

时间:2015-04-30 02:58:16

标签: android crash android-edittext

当我的EditText字段为空时,如何防止我的应用崩溃?

    public class Credits extends Activity {
    int inaugural = 1992;
    int differenceInYears;
    int userGuess;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
        final EditText Guess=(EditText)findViewById(R.id.txtYearGuess);
        Button go = (Button)findViewById(R.id.btnCalculate);
        final TextView result = ((TextView)findViewById(R.id.txtResult));

        go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int userGuess= Integer.parseInt(Guess.getText().toString());
                differenceInYears = year - inaugural;
                output = userGuess - differenceInYears;

                if (output < -1) {
                    result.setText("Guess again! You guessed too low!");

                    }

                else if (output == 1) {
                    result.setText("You're REALLY close! You guessed too high!");


                    }

                else if (output == -1) {
                    result.setText("You're REALLY close! You guessed too low!");


                    }

                else if (output > 1) {
                    result.setText("Guess again! You guessed too high!");


                    }

                else {
                    result.setText("Good job! You're an FRC Genious!");

                    }



            }

        });
    }

}

是否只需要另一个if语句来观察&#34;空&#34;变量?如果是这样,那个代码会是什么样的?我对防止这次崩溃发生的想法感到茫然。如果有任何类型的Eclipse报告可以帮助回答这个问题,请告诉我在哪里找到它。

谢谢!

修改

我尝试添加推荐的TextUtils,但他们没有解决应用程序崩溃问题。

public class Credits extends Activity {
    int inaugural = 1992;
    int differenceInYears;
    int userGuess;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
        final EditText Guess=(EditText)findViewById(R.id.txtYearGuess);
        Button go = (Button)findViewById(R.id.btnCalculate);
        final TextView result = ((TextView)findViewById(R.id.txtResult));

        go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int userGuess= Integer.parseInt(Guess.getText().toString());
                differenceInYears = year - inaugural;
                output = userGuess - differenceInYears;

                if(!TextUtils.isEmpty(Guess.getText().toString())) {
                    Toast.makeText(Credits.this, "Please input your Guess", Toast.LENGTH_LONG).show();
                    }

                else {

                if (output < -1) {
                    result.setText("Guess again! You guessed too low!");

                    }

                else if (output == 1) {
                    result.setText("You're REALLY close! You guessed too high!");


                    }

                else if (output == -1) {
                    result.setText("You're REALLY close! You guessed too low!");


                    }

                else if (output > 1) {
                    result.setText("Guess again! You guessed too high!");


                    }

                else {
                    result.setText("Good job! You're an FRC Genious!");

                    }

            }

            }

        });
    }

}

2 个答案:

答案 0 :(得分:0)

在onClickListener的onClick方法中添加检查

    String text = Guess.getText().toString();
    if(text != null && !text.trim().equals("")){
       try{
            //Do watever you want to do here
       }catch(NumberFormatException e){
            e.printStackTrace();
       }
    }

您也在进行int解析,因此您还可以检查TextUtils.isDigitsOnly(Doc

答案 1 :(得分:0)

getText().getString()保证永远不会返回null,只要你的edittext被正确初始化(即它指向实际视图),所以最坏的情况下你会得到一个空字符串 - &gt; ""。要检查这一点,请按照

的方式执行操作

if (!Guess.getText().getString().equals(""))如果获得的值不等于“”,则执行填充。

您可能还希望将解析语句包装在try-catch块中,以防止人们输入字母或其他内容。

public static Integer tryParse(String text) {
   try {
     return new Integer(text);
   } catch (NumberFormatException e) {
     return null;
   }
}

try / parse的来源:here