Reputation: 67
I have made a simple Android app, where the app creates a number between 1 and 20 and the user has to guess it.
Everything works fine, except one thing: When I let the EditText
empty, the App crashes instantly when I press the Button. How can I prevent that? I tried an if-else
Argument at the point where the entered Text of the EditText
is given to the global created editText
Variable editTextGuess
, but then the app crashed instantly after launch.
What do I have to Code, that the app does not crash caused by the empty textfield but creates a toast saying "Oops, your Textfield is empty"?
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();
}
}
Upvotes: 2
Views: 321
Reputation: 2462
As the issue has already been pointed out by @Yousaf this answer that will help you move forward but would like to add few things to take care in future to help debugging easier: Always try to create multiple statements where you are dereferencing object like
guessValue = Integer.parseInt(editTextGuess.getText().toString());
can be written as
String text=editTextGuess.getText().toString();
guessValue = Integer.parseInt(text);
It will help you know the exact cause of failure.Like you may encounter NumberFormatException
if the entered string is not number.
Upvotes: 0
Reputation: 23017
It crashes because parseInt()
throws a NumberFormatException
when it tries to parse an empty string.
How to fix this depends on what you want if the input field is empty.
String value = editTextGuess.getText().toString();
if (!value.isEmpty()) {
guessValue = Integer.parseInt(value);
...
}
else {
Toast.makeText(this, "Oops, input field is empty", ...);
}
Upvotes: 0
Reputation: 1997
Check your edit text string value before assigning it to the variable.
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();
}
}
Upvotes: 0
Reputation: 29314
Before you try to use text from EditText
, check if it contains any text or not. If it does, read the text otherwise display a toast
String text = editTextGuess.getText().toString().trim();
if(text.length == 0) {
// show toast
} else {
// use text from editTextGuess
}
Upvotes: 2