简体   繁体   English

空的 EditText 崩溃 Android

[英]Empty EditText crash Android

I just started learning Android and I made a code that gives you an equation and you need to solve it, it's very simple but if someone presses the check button and the edit text is empty the app crashes and I want it to count it as mistake.. here is my code :我刚开始学习 Android,我做了一个代码,给你一个方程,你需要解决它,这很简单,但是如果有人按下复选按钮并且编辑文本为空,应用程序就会崩溃,我希望它把它算作错误.. 这是我的代码:

   public void Generate(View v) {
    x1=10+(int)((99-10+1)*Math.random());
    x2=10+(int)((99-10+1)*Math.random());
    tv1.setText("" + x1 + "+" + x2 + "=" + "?");
}

public void Check(View view) {
    answer1 = et1.getText().toString();
    answer = Integer.parseInt(answer1);
    if (answer == (x1 + x2)) {
        count1++;
        count2++;
        Toast.makeText(getApplicationContext(), "Correct !",
                Toast.LENGTH_SHORT).show();
    }


    if(answer!=(x1+x2))
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Wrong !",
                Toast.LENGTH_SHORT).show();
    }

    if(answer1=="")
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Don't try to cheat !",
                Toast.LENGTH_SHORT).show();
    }

    et1.setText("");
    tv2.setText("Number of questions : "+count1);
    tv3.setText("Right answers : "+count2);
    tv4.setText("Wrong answers : "+count3);

    Generate(view);
}

The thing is that you are converting et1.getText() to String and if this returns null value, null pointer exception is raised.问题是您正在将 et1.getText() 转换为 String,如果返回空值,则会引发空指针异常。 so need to implement a check whether the value is null or not :所以需要检查值是否为空:

if(et1.getText()!=null){

answer1 = et1.getText().toString();
if(et1.getText().toString().length()>0){
    answer = Integer.parseInt(answer1);
}
    if (answer == (x1 + x2)) {
        count1++;
        count2++;
        Toast.makeText(getApplicationContext(), "Correct !",
                Toast.LENGTH_SHORT).show();
    }


    if(answer!=(x1+x2))
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Wrong !",
                Toast.LENGTH_SHORT).show();
    }

    if(answer1=="")
    {
        count1++;
        count3++;
        Toast.makeText(getApplicationContext(), "Don't try to cheat !",
                Toast.LENGTH_SHORT).show();
    }

    et1.setText("");
    tv2.setText("Number of questions : "+count1);
    tv3.setText("Right answers : "+count2);
    tv4.setText("Wrong answers : "+count3);

    Generate(view);
}

Another thing is that if String is empty then it will be unable to parse to int so you also need check for that另一件事是,如果 String 为空,那么它将无法解析为 int,因此您还需要检查

either任何一个

if(et1.getText().toString().length()>0){
        answer = Integer.parseInt(answer1);
    }

or或者

if(!TextUtils.isEmpty(answer1)){
answer = Integer.parseInt(answer1);
}

You can check if the EditText is empty .您可以检查 EditText 是否为空。 Do it as below, in your code在您的代码中执行以下操作

if (!et1.getText().toString().matches(""))
{
    answer1 = et1.getText().toString();
}
else
{
    //No value entered
}

OR simply或者干脆

if (!et1.getText().toString().equals(""))
{
    answer1 = et1.getText().toString();
}
else
{
    //No value entered
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM