java - Converting String to Integer in Android application -
i trying convert string integer using edittext field in android, application crashes whenever try click button. string section work issue when user types integer in edittext field named number.
package com.example.addingvalu; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity { edittext number,txtval; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void viewanumber(view v1) { txtval = (edittext) findviewbyid(r.id.stringvalue); number = (edittext) findviewbyid(r.id.numbers_text); //string ab; int ed; ed = integer.parseint(number.gettext().tostring()); if(txtval.length() != 0) { toast.maketext(getbasecontext(), "string " + txtval.gettext().tostring() ,toast.length_long).show(); } else if(number.length() !=0) { toast.maketext(getbasecontext(),ed, toast.length_long).show(); } } }
it practice not start methods , attributes uppercase. add try/catch print possible error out , have clues on how fix it. try:
public class mainactivity extends activity { edittext number,txtval; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void viewanumber(view v1) { txtval = (edittext) findviewbyid(r.id.stringvalue); number = (edittext) findviewbyid(r.id.numbers_text); //string ab; int ed; ed = 0; try{ ed = integer.parseint(number.gettext().tostring()); }catch(exception e){ e.printstacktrace(); } if(txtval.length() != 0) { toast.maketext(getbasecontext(), "string " + txtval.gettext().tostring() ,toast.length_long).show(); } else if(number.length() !=0) { toast.maketext(getbasecontext(),ed, toast.length_long).show(); } } }
Comments
Post a Comment