Reputation: 35
Is it a proper casting? After pressing button the app is closing. If I toggle comment on second line (height = Integer
) then it works fine.
public void onClick(View v)
{
EditText tHeight = (EditText)findViewById(R.id.tbHeight);
height = Integer.parseInt(tHeight.getText().toString());
}
Full log if you mean this log:
02-09 14:41:58.941: I/dalvikvm(15417): Turning on JNI app bug workarounds for target SDK version 5...
02-09 14:42:03.918: D/AndroidRuntime(15417): Shutting down VM
02-09 14:42:03.918: W/dalvikvm(15417): threadid=1: thread exiting with uncaught exception (group=0x40bf11f8)
02-09 14:42:03.918: E/AndroidRuntime(15417): FATAL EXCEPTION: main
02-09 14:42:03.918: E/AndroidRuntime(15417): java.lang.NumberFormatException: Invalid int: ""
02-09 14:42:03.918: E/AndroidRuntime(15417): at java.lang.Integer.invalidInt(Integer.java:138)
02-09 14:42:03.918: E/AndroidRuntime(15417): at java.lang.Integer.parseInt(Integer.java:359)
02-09 14:42:03.918: E/AndroidRuntime(15417): at java.lang.Integer.parseInt(Integer.java:332)
02-09 14:42:03.918: E/AndroidRuntime(15417): at com.example.as.MainActivity$1.onClick(MainActivity.java:64)
02-09 14:42:03.918: E/AndroidRuntime(15417): at android.view.View.performClick(View.java:3558)
02-09 14:42:03.918: E/AndroidRuntime(15417): at android.view.View$PerformClick.run(View.java:14157)
02-09 14:42:03.918: E/AndroidRuntime(15417): at android.os.Handler.handleCallback(Handler.java:605)
02-09 14:42:03.918: E/AndroidRuntime(15417): at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 14:42:03.918: E/AndroidRuntime(15417): at android.os.Looper.loop(Looper.java:137)
02-09 14:42:03.918: E/AndroidRuntime(15417): at android.app.ActivityThread.main(ActivityThread.java:4514)
02-09 14:42:03.918: E/AndroidRuntime(15417): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 14:42:03.918: E/AndroidRuntime(15417): at java.lang.reflect.Method.invoke(Method.java:511)
02-09 14:42:03.918: E/AndroidRuntime(15417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-09 14:42:03.918: E/AndroidRuntime(15417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-09 14:42:03.918: E/AndroidRuntime(15417): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 2
Views: 96
Reputation: 39836
Remember that Integer.parseInt
can throw NumberFormatException
. That is probably what is happening.
Do you have a number on the EditText
? It's likely that if there's a space it won't parse as well. Try this tHeight.getText().toString().trim().replace(" ", "");
to remove any spaces.
Also on the XML layout, make sure to mark the input type as numeric.
And last but not least, make sure to put a try{ }catch(NumberFormatException e){ }
to make sure it won't crash
Upvotes: 2