0

I am new to Android programming, and I am making a number guessing game to get to know the basic Android code. In my program, I have an EditText (basically a TextField) and I was wondering how I would change whatever the user inputs into an int. So far I have tried Integer.parseInt(string), but it didn't work. Here is the code that I have right now:

EditText textfield = (EditText) findViewById(R.id.textfield);
String label = textfield.getText().toString();
int guess = Integer.parseInt(label);

When I run this program to debug on my phone, it all works fine until I press the button that I call this code with. When I press the button, the program closes and a popup comes that says

"The application Test (process com.android.test) has stopped unexpectedly. Please try again."

This happens every time I try to run the program. I have debugged, and I have isolated the problem to the line of code that parses the int. How do I make this work?

4
  • What's the value of label at the Integer.parseInt() line? Commented Mar 2, 2011 at 1:10
  • If you know exactly which line triggers the error, why don't you put a try...catch around it and analyze the exception? Commented Mar 2, 2011 at 1:18
  • 1
    Can you post the stack trace? This is for easier debugging. Commented Mar 2, 2011 at 1:21
  • Well OP should definitely know what he typed into the edit field. If it's not a number, then he'd get an exception as others have stated. But if it's just the number '9' he/she typed, there's something else going on. Just tell us what you typed into the edit field. Commented Mar 2, 2011 at 1:34

2 Answers 2

1

You're likely getting a NumberFormatException, but that isn't enough to help you except temporarily.

What you need to do is become familiar with some of the Android debugging facilities.

Start the phone emulator using the platform tools.

Open a command prompt and navigate to your android-sdk\platform-tools directory.

Run adb logcat to connect to the running emulator. In the emulator, run your program and cause the error to occur. You will get a stack trace in your command line window to help you isolate your problem.

Check out how to use LogCat on the dev site.

Sign up to request clarification or add additional context in comments.

Comments

0

Whenever you get a crash, you need to print out the callstack. That shows you exactly what's wrong.

In this case, maybe you're not entering a number? Whenever you're parsing a string, be sure to put a try/catch around the Integer.parseInt, or else the user can crash your app by entering something that is not a number.

The only other options would be that R.id.textfield is not an EditText, or there is no R.id.textfield in this particular layout. If you had printed the stack trace, we'd know for sure.

1 Comment

Thanks. What exception should I catch?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.