Android - App crashing because of TextWatcher -


when write number in edittext works fine when delete number app crashes.

this 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 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: ""         @ java.lang.integer.invalidint(integer.java:137)         @ java.lang.integer.parseint(integer.java:358)         @ java.lang.integer.parseint(integer.java:331)         @ com.app.boyd44.myfirstapp.myactivity$1.aftertextchanged(myactivity.java:68)         @ android.widget.textview.sendaftertextchanged(textview.java:7445)         @ android.widget.textview$changewatcher.aftertextchanged(textview.java:9232)         @ android.text.spannablestringbuilder.sendaftertextchanged(spannablestringbuilder.java:970)         @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:497)         @ android.text.spannablestringbuilder.delete(spannablestringbuilder.java:212)         @ android.text.spannablestringbuilder.delete(spannablestringbuilder.java:30)         @ android.text.method.basekeylistener.backspaceorforwarddelete(basekeylistener.java:94)         @ android.text.method.basekeylistener.backspace(basekeylistener.java:49)         @ android.text.method.basekeylistener.onkeydown(basekeylistener.java:155)         @ android.text.method.numberkeylistener.onkeydown(numberkeylistener.java:138)         @ android.widget.textview.dokeydown(textview.java:5553)         @ android.widget.textview.onkeydown(textview.java:5360)         @ android.view.keyevent.dispatch(keyevent.java:2663)         @ android.view.view.dispatchkeyevent(view.java:7796)         @ android.view.viewgroup.dispatchkeyevent(viewgroup.java:1408)         @ android.view.viewgroup.dispatchkeyevent(viewgroup.java:1408)         @ android.view.viewgroup.dispatchkeyevent(viewgroup.java:1408)         @ android.view.viewgroup.dispatchkeyevent(viewgroup.java:1408)         @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchkeyevent(phonewindow.java:2039)         @ com.android.internal.policy.impl.phonewindow.superdispatchkeyevent(phonewindow.java:1509)         @ android.app.activity.dispatchkeyevent(activity.java:2421)         @ com.android.internal.policy.impl.phonewindow$decorview.dispatchkeyevent(phonewindow.java:1966)         @ android.view.viewrootimpl$viewpostimeinputstage.processkeyevent(viewrootimpl.java:3875)         @ android.view.viewrootimpl$viewpostimeinputstage.onprocess(viewrootimpl.java:3849)         @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3416)         @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3466)         @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3435)         @ android.view.viewrootimpl$asyncinputstage.forward(viewrootimpl.java:3542)         @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3443)         @ android.view.viewrootimpl$asyncinputstage.apply(viewrootimpl.java:3599)         @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3416)         @ android.view.viewrootimpl$inputstage.ondelivertonext(viewrootimpl.java:3466)         @ android.view.viewrootimpl$inputstage.forward(viewrootimpl.java:3435)         @ android.view.viewrootimpl$inputstage.apply(viewrootimpl.java:3443)         @ android.view.viewrootimpl$inputstage.deliver(viewrootimpl.java:3416)         @ android.view.viewrootimpl.deliverinputevent(viewrootimpl.java:5565)         @ android.view.viewrootimpl.doprocessinputevents(viewrootimpl.java:5545)         @ android.view.viewrootimpl.enqueueinputevent(viewrootimpl.java:5516)         @ android.view.viewrootimpl$viewroothandler.handlemessage(viewrootimpl.java:3187)         @ android.os.handler.dispatchmessage(handler.java:106)         @ android.os.looper.loop(looper.java:136)         @ android.app.activitythread.main(activitythread.java:5212)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:515)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602)         @ dalvik.system.nativestart.main(native method) 

the crash has textwatcher because crashes when deleting in edittext.

i don't know how fix please help.

the textchanged gets called when delete content. when content empty. check condition , return.

decimalformat df = new decimalformat("#.00"); if(s.tostring().trim().length()==0){return}             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)); 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -