Reputation: 15
I am trying to write a programm that will get the number input in the EditText field and convert it to integer so that I could use it later to set a timer for the app. This is the code I have. When i run it, it gives me an error: "Android java.lang.NumberFormatException: Invalid int". Where is my mistake? Thanks in advance
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Start extends Activity {
Button button1;
EditText et;
String timer;
int timer1;
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
button1 = (Button)findViewById(R.id.button1);
et = (EditText)findViewById(R.id.editText1);
bull();
button1.setOnClickListener ( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Start.this, MainActivity.class);
startActivity(intent);
}
}, secondsDelayed * timer1*1000);
}
});
}
private void bull() {
// TODO Auto-generated method stub
timer1 = Integer.valueOf(et.getText().toString());
}
}
Here is my LogCat:
03-16 01:39:10.663: I/Process(17577): Sending signal. PID: 17577 SIG: 9
03-16 01:39:10.853: D/AndroidRuntime(18166): Shutting down VM
03-16 01:39:10.853: W/dalvikvm(18166): threadid=1: thread exiting with uncaught exception (group=0x4104fac8)
03-16 01:39:10.863: E/AndroidRuntime(18166): FATAL EXCEPTION: main
03-16 01:39:10.863: E/AndroidRuntime(18166): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scaryme/com.example.scaryme.Start}: java.lang.NumberFormatException: Invalid int: ""
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.access$700(ActivityThread.java:152)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.os.Looper.loop(Looper.java:137)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.main(ActivityThread.java:5328)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-16 01:39:10.863: E/AndroidRuntime(18166): at dalvik.system.NativeStart.main(Native Method)
03-16 01:39:10.863: E/AndroidRuntime(18166): Caused by: java.lang.NumberFormatException: Invalid int: ""
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.Integer.invalidInt(Integer.java:138)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.Integer.parseInt(Integer.java:359)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.Integer.parseInt(Integer.java:332)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.example.scaryme.Start.bull(Start.java:48)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.example.scaryme.Start.onCreate(Start.java:24)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.Activity.performCreate(Activity.java:5250)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-16 01:39:10.863: E/AndroidRuntime(18166): ... 11 more
Upvotes: 1
Views: 2215
Reputation: 40396
In your onCreate()
method you have:
public void onCreate (Bundle savedInstanceState) {
...
et = (EditText)findViewById(R.id.editText1);
bull()
...
}
The initial contents of the EditText
is an empty string. However, you immediately call bull()
, which does:
timer1 = Integer.valueOf(et.getText().toString());
Since et
contains an empty string (remember, you've called this from onCreate()
and the edit box is empty), Integer.valueOf()
cannot parse it, because an empty string is not a valid integer. Integer.valueOf()
will then throw the exception you are seeing.
You have a few immediate options:
bull()
from onCreate()
, orbull()
not attempt to parse the string if the edit text is empty.I recommend the first, since it does not seem like you need to do that (at least in your example).
In any case, during normal operation, it is always possible for the user to accidentally enter an invalid number, and so you will want to handle that error gracefully by catching the exception and displaying a useful error message to the user (e.g. "Please enter a valid integer!").
In general, you need to look at the stack traces when you get exceptions like this. The onCreate()
-> bull()
-> exception code path is clearly shown by your stack trace, and it also identifies precisely which line the exception and calls occurred on. Once you identify the source of the exception, it usually then helps to consult the documentation for the method that is throwing it, which generally describes why the exception is thrown (and then, knowing why, you can solve the problem). For example, Integer.parseInt()
documentation. Stack traces are one of the most important pieces of information for finding the cause of unexpected exceptions.
Upvotes: 1
Reputation: 2698
I guess your EditText
doesn't contain a well-formated number. Could you provide some examples of the input values?
In the meantime check out these examples of Integer.parseInt
:
public static void main(String args[]) throws Exception {
String valid = "925";
int parsed = Integer.parseInt(valid); // works
String invalid = "Hello World!";
int error = Integer.parseInt(invalid); // throws NumberFormatException
String empty = "";
int error2 = Integer.parseInt(empty); // throws NumberFormatException
}
Upvotes: 1
Reputation: 137
Try this:
timer1 = Integer.parseInt(et.getText().toString());
Upvotes: -1