i working on activity register page input info , store in object.
this problem arose when tried implement check boxes code.
is there solution this?
public class register extends appcompatactivity implements view.onclicklistener { button bregister, bback; edittext etname, etage, etusername, etpassword; checkbox cbaccount; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); // user info etname = (edittext) findviewbyid(r.id.etname); etage = (edittext) findviewbyid(r.id.etage); etusername = (edittext) findviewbyid(r.id.etusername); etpassword = (edittext) findviewbyid(r.id.etpassowrd); bregister = (button) findviewbyid(r.id.bregister); bback = (button) findviewbyid(r.id.bback); cbaccount = (checkbox)findviewbyid(r.id.cbaccount); bregister.setonclicklistener(this); bback.setonclicklistener(this); cbaccount.setonclicklistener(this); } boolean account = false; @override public void onclick(view view) { boolean checked = ((checkbox) view).ischecked(); //"switch" if statement, 1 can put multiple "cases" switch(view.getid()) { // when user registers, store info case r.id.bregister: // store user info string name = etname.gettext().tostring(); string username = etusername.gettext().tostring(); string password = etpassword.gettext().tostring(); int age = integer.parseint(etage.gettext().tostring()); boolean accountsend = account; // call method store user info login.java class authentication userlocalstorage uls = userlocalstorage.getinstance(); uls.setusername(username); uls.setpassword(password); uls.setname(name); uls.setage(age); uls.setaccount(accountsend); startactivity(new intent(this, login.class)); break; case r.id.bback: startactivity(new intent(this, login.class)); break; case r.id.cbaccount: if (checked) { account = true; } else { account = false; } break; } } }
here logcat
01-24 20:43:15.971 8074-8074/com.example.huynh.nom e/androidruntime: fatal exception: main
process: com.example.huynh.nom, pid: 8074
java.lang.classcastexception: android.support.v7.widget.appcompatbutton cannot cast android.widget.checkbox
@ com.example.huynh.nom.register.onclick(register.java:44)
@ android.view.view.performclick(view.java:5198)
@ android.view.view$performclick.run(view.java:21147)
@ android.os.handler.handlecallback(handler.java:739)
@ android.os.handler.dispatchmessage(handler.java:95)
@ android.os.looper.loop(looper.java:148)
@ android.app.activitythread.main(activitythread.java:5417)
@ java.lang.reflect.method.invoke(native method)
@ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726)
@ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
in onclick
handler have following line:
boolean checked = ((checkbox) view).ischecked();
since registered same handler used multiple different views not work since not checkboxes.
remove line , can instead:
case r.id.cbaccount: if (cbaccount.ischecked()) { account = true; } else { account = false; } break;
Comments
Post a Comment