Kerv Kerv - 5 months ago 16
Android Question

App crashes when I entered nothing on edittext

What's missing in code?

I have a problem here. I have here 2 edittext. One for amount and one for password. My app works like a charm except when I entered NONE on the edittexts, IT CRASHES (Unfortunately stop). Am I missing any code on my activity? Please help. Here's my code.

public class MainActivity extends Activity {
Button REDIRECT;
private EditText txtbox1,txtbox2;

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

REDIRECT = (Button) findViewById(R.id.button1);
txtbox1= (EditText) findViewById(R.id.editText1);
txtbox2= (EditText) findViewById(R.id.editText2);

REDIRECT.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int Amount = Integer.parseInt(txtbox1.getText().toString());
String Password = txtbox2.getText().toString();


if(Amount<=50&&Amount>=1 & Password.equals("TUBOL"))
{

final Intent i = new Intent(MainActivity.this, Redirect.class);
startActivity(i);
}
else
{
Toast.makeText(MainActivity.this, "INVALID", Toast.LENGTH_LONG).show();
}
}
});

}


Here is my logcat:

09-15 01:46:34.641: E/AndroidRuntime(30540): FATAL EXCEPTION: main
09-15 01:46:34.641: E/AndroidRuntime(30540): java.lang.NumberFormatException: Invalid int: ""
09-15 01:46:34.641: E/AndroidRuntime(30540): at java.lang.Integer.invalidInt(Integer.java:138)
09-15 01:46:34.641: E/AndroidRuntime(30540): at java.lang.Integer.parseInt(Integer.java:359)
09-15 01:46:34.641: E/AndroidRuntime(30540): at java.lang.Integer.parseInt(Integer.java:332)
09-15 01:46:34.641: E/AndroidRuntime(30540): at com.example.pwordlockdown.MainActivity$1.onClick(MainActivity.java:29)
09-15 01:46:34.641: E/AndroidRuntime(30540): at android.view.View.performClick(View.java:3549)
09-15 01:46:34.641: E/AndroidRuntime(30540): at android.view.View$PerformClick.run(View.java:14400)
09-15 01:46:34.641: E/AndroidRuntime(30540): at android.os.Handler.handleCallback(Handler.java:605)
09-15 01:46:34.641: E/AndroidRuntime(30540): at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 01:46:34.641: E/AndroidRuntime(30540): at android.os.Looper.loop(Looper.java:154)
09-15 01:46:34.641: E/AndroidRuntime(30540): at android.app.ActivityThread.main(ActivityThread.java:4945)
09-15 01:46:34.641: E/AndroidRuntime(30540): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 01:46:34.641: E/AndroidRuntime(30540): at java.lang.reflect.Method.invoke(Method.java:511)
09-15 01:46:34.641: E/AndroidRuntime(30540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-15 01:46:34.641: E/AndroidRuntime(30540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-15 01:46:34.641: E/AndroidRuntime(30540): at dalvik.system.NativeStart.main(Native Method)

Answer

It's an uncaught NumberFormatExpection on Integer.parseInt(txtbox1.getText().toString())

txtbox1 is empty when this method is called, so you're calling Integer.parseInt("") which throws a NumberFormatExpection.