当EditText为空时,Java简单数字游戏应用程序崩溃

时间:2019-02-09 09:21:10

标签: java android

我制作了一个简单的Android应用程序,其中该应用程序创建了一个介于1到20之间的数字,用户必须猜测它。 一切正常,除了一件事:当我将EditText设为空时,按下按钮时应用程序立即崩溃。我该如何预防?在将if-else的输入文本提供给全局创建的EditText变量editText的时候,我尝试了一个editTextGuess参数,但是应用程序在启动后立即崩溃。

我需要编写什么代码,该应用程序不会因文本字段为空而崩溃,而是创建一个吐司说“糟糕,您的文本字段为空”?

package com.example.randomness;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    //Den EditText und den Integer für die zufällige Zahl global erstellen
    EditText editTextGuess;
    int RandomNumber;

    //Hier einen Void eröffnen, um die zufällige Zahl zu erstellen
    public void generateRandomNumber(){
        Random random = new Random();

        RandomNumber = random.nextInt(20) +1;
    }

    //Erstellen der OnClick Methode für den Button, View view ist wichtig!
    public void guess(View view){
        Log.i("Button clicked", "Worked!");

        int guessValue;
        guessValue = Integer.parseInt(editTextGuess.getText().toString());
        Log.i("Entered Value:", Integer.toString(guessValue));
        Log.i("The random number is:", Integer.toString(RandomNumber));


        String message;

        if(guessValue > RandomNumber){
            message = "Your guessed number is too high!";
        } else if(guessValue < RandomNumber){
            message = "Your guessed number is too low!";
        } else if(guessValue == RandomNumber){
            message = "You were right! Let's play again!";
            generateRandomNumber();
            Log.i("Info", "New random number created");
        } else {
            message = "Something went wrong...";
        }

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextGuess = findViewById(R.id.editTextRaten);
        generateRandomNumber();

    }
}

5 个答案:

答案 0 :(得分:2)

在尝试使用EditText中的文本之前,请检查其中是否包含任何文本。如果是这样,请阅读文本,否则显示toast

String text = editTextGuess.getText().toString().trim();

if(text.length == 0) {
  // show toast
} else {
  // use text from  editTextGuess
}

答案 1 :(得分:0)

它崩溃是因为parseInt()在尝试解析空字符串时抛出了NumberFormatException

如何解决此问题取决于输入字段为空的情况。

String value = editTextGuess.getText().toString();
if (!value.isEmpty()) {
    guessValue = Integer.parseInt(value);
    ...
}
else {
    Toast.makeText(this, "Oops, input field is empty", ...);
}

答案 2 :(得分:0)

在将其分配给变量之前,请检查您的编辑文本字符串值。

public void guess(View view){
    Log.i("Button clicked", "Worked!");

    int guessValue;
    if(!editTextGuess.getText().toString().equals("")){
        guessValue = Integer.parseInt(editTextGuess.getText().toString());
        Log.i("Entered Value:", Integer.toString(guessValue));
        Log.i("The random number is:", Integer.toString(RandomNumber));


        String message;

        if(guessValue > RandomNumber){
            message = "Your guessed number is too high!";
        } else if(guessValue < RandomNumber){
            message = "Your guessed number is too low!";
        } else if(guessValue == RandomNumber){
            message = "You were right! Let's play again!";
            generateRandomNumber();
            Log.i("Info", "New random number created");
        } else {
            message = "Something went wrong...";
        }

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }  else {
        Toast.makeText(this, "Oops, your Textfield is empty", Toast.LENGTH_LONG).show();
    }

}

答案 3 :(得分:0)

如果edit-text为空,那么您将需要这样的条件,否则它不会让您的代码运行,您还可以祝酒,让用户在其他部分输入数据。

 if(!editTextGuess.getText().toString().equals("")){
    guessValue = Integer.parseInt(editTextGuess.getText().toString());         
    Log.i("Entered Value:", Integer.toString(guessValue));
    Log.i("The random number is:", Integer.toString(RandomNumber));

    ......here goes your rest code

 }

答案 4 :(得分:0)

@Yousaf this answer已经指出了这个问题,它可以帮助您继续前进,但希望在将来增加一些注意事项以帮助简化调试: 始终尝试在要取消引用对象的地方创建多个语句,例如

guessValue = Integer.parseInt(editTextGuess.getText().toString());

可以写为

String text=editTextGuess.getText().toString();
guessValue = Integer.parseInt(text);

这将帮助您了解失败的确切原因。就像输入的字符串不是数字一样,您可能会遇到NumberFormatException