简体   繁体   English

当 EditText 为空时 Android 应用程序崩溃

[英]Android application crashes when EditText is empty

I have a problem, that i can't solve, and solution of teacher doesn't work.我有一个问题,我无法解决,老师的解决方案不起作用。

The problem is when I click "Check the answer" and nothing is written in EditText , the program crashes.问题是当我单击“检查答案”并且EditText没有写入任何内容时,程序崩溃。 I work in Android Studio.我在 Android Studio 工作。

Java code:爪哇代码:

public class Main extends AppCompatActivity {
    int a, b, c, d;
    Button Enter, NewExercise, EndLesson;
    EditText et;
    TextView tv;
    ImageView iv;
    String st;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        et = (EditText) findViewById(R.id.et);
        iv = (ImageView) findViewById(R.id.iv);
        a = (int)(10 * Math.random());
        b = (int)(10 * Math.random());
        c = a + b;
        tv.setText("Exmaple: " + a + " + " + b);
        et.setText("" + c);
    }

    public void Show(View view) {
        a = (int)(10 * Math.random());
        b = (int)(10 * Math.random());
        c = a + b;
        tv.setText("" + a + " + " + b);
        et.setText("");
    }

    public void Check(View view) {
        st = et.getText().toString();
        d = Integer.parseInt(st);
        if(0 == et.length()) {
            Toast.makeText(this,"Please, enter the answer",Toast.LENGTH_LONG).show();
        }
        if (d == c) {
            iv.setImageResource(R.drawable.t);
            Toast.makeText(this, "Right", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "False", Toast.LENGTH_LONG).show();
            iv.setImageResource(R.drawable.f);
            et.setText("");
        }
    }
}

Xml: xml:

<TextView
    android:layout_width="wrap_content"
    android:textSize="30dp"
    android:textColor="#000000"
    android:layout_height="wrap_content"
    android:id="@+id/tv" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="71dp"
    app:srcCompat="@drawable/eq"
    android:id="@+id/eq" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:textSize="30dp"
    android:hint="Enter the answer"
    android:ems="10"
    android:maxLength="3"
    android:gravity="center"
    android:id="@+id/et"
    android:layout_weight="0.13" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:srcCompat="@drawable/t"
    android:id="@+id/iv"
    android:layout_weight="0.12" />

<Button
    android:text= "Check the answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Check"
    android:id="@+id/CheckTheAnswer" />

<Button
    android:text= "New Exercise"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Show"
    android:id="@+id/NewExercise" />

<Button
    android:text="End lesson"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Exit"
    android:id="@+id/EndLesson" />

I'm sorry for my English.我为我的英语感到抱歉。 Thank you in advance.提前谢谢你。

That's because you are parsing empty string (ie "") to int which is causing crash.那是因为您正在将空字符串(ie "")解析为导致崩溃的 int。

You should carefully handle exception while parsing String to int using try-catch, because if String can't be parsed into int then it throw Runtime error, which will cause application to crash.使用 try-catch 将 String 解析为 int 时应小心处理异常,因为如果 String 无法解析为 int 则抛出 Runtime 错误,这将导致应用程序崩溃。

Make a check to the EditText if it is empty or not using this:使用此检查 EditText 是否为空:

protected boolean isEmpty(EditText editText) {
        return (editText.getText().toString().equals(""));
    }

Hope this helps.希望这会有所帮助。

Try putting a null checker.尝试放置一个空检查器。 Example:示例:

if(et != null){
  st = et.getText().toString().trim();
       d = Integer.parseInt(st);
}

I added trim() method as well to trim trailing and leading white spaces.我还添加了 trim() 方法来修剪尾随和前导空格。

Thanks for answers, but only this solution work.感谢您的回答,但只有这个解决方案有效。

if (st.equals(""))
    {
  vbsays.setText("< First, you need to enter something into the text box, and then check your answer. Try!");
    }
    else 
{
        d = Integer.parseInt(st);
}

the return type of getText() is editable so we need to convert it into string by using toString(). getText() 的返回类型是可编辑的,因此我们需要使用 toString() 将其转换为字符串。

if(t1.getText().toString().equals("")// t1 is Edittext(class) object
   {
       Toast.makeText(MainActivity.this, "my Message", Toast.LENGTH_LONG).show();
   }
else
   {
       //do ur operation
   }  

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

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