When I write a number in the editText it works fine but when I delete the number the app crashes.
This is the code:
final EditText et2 = (EditText) findViewById(R.id.editText2);
final EditText et3 = (EditText) findViewById(R.id.editText3);
et2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("#.00");
String et2string = et2.getText().toString();
int et2int = Integer.parseInt(et2string);
Double finpriceAdulto = (priceAdulto * et2int);
TextView tvPriceAdulto = (TextView) findViewById(R.id.textView7);
tvPriceAdulto.setText("" + df.format(finpriceAdulto));
}
});
et3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("#.00");
String et3string = et3.getText().toString();
int et3int = Integer.parseInt(et3string);
Double finpriceNino = (priceNino * et3int);
TextView tvPriceNino = (TextView) findViewById(R.id.textView8);
tvPriceNino.setText("" + df.format(finpriceNino));
}
});
And this is the logcat error:
06-26 13:32:07.835 6321-6321/com.app.boyd44.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.app.boyd44.myfirstapp, PID: 6321
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.app.boyd44.myfirstapp.MyActivity$1.afterTextChanged(MyActivity.java:68)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7445)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9232)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
at android.text.method.BaseKeyListener.backspaceOrForwardDelete(BaseKeyListener.java:94)
at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:49)
at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:155)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:138)
at android.widget.TextView.doKeyDown(TextView.java:5553)
at android.widget.TextView.onKeyDown(TextView.java:5360)
at android.view.KeyEvent.dispatch(KeyEvent.java:2663)
at android.view.View.dispatchKeyEvent(View.java:7796)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2039)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1509)
at android.app.Activity.dispatchKeyEvent(Activity.java:2421)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1966)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3875)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3849)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3466)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3435)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3542)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3443)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3599)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3466)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3435)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3443)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3416)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5565)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5545)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5516)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3187)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
The crash has clearly something to do with TextWatcher because it crashes when deleting something in the editText.
I don't know how to fix this so please help.
The textchanged gets called even when you delete the content. So when your content is empty. Check for the condition and return.