radio button - RadioButton - Checked if radiobutton is checked inside a radiogroup - Android -
i'm having problem checking if 1 of radiobutton
selected inside inside radiogroup
, i've tried lot of methods if(rg.getcheckedradiobuttonid() == -1)
still not working on code. on logcat string selectedanstext = selectedans.gettext().tostring();
in line logcat pointing problem, can me. tried .ischecked
not working. in advanced
bt.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { radiobutton selectedans = (radiobutton) findviewbyid(rg.getcheckedradiobuttonid()); string selectedanstext = selectedans.gettext().tostring(); if(rg.getcheckedradiobuttonid()== -1){ toast.maketext(grade_four_post_test.this, "choose answer", toast.length_long).show(); }else { //do }
ps: bt
declared properly
01-28 22:27:07.390 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ android.view.view.performclick(view.java:4463) 01-28 22:27:07.391 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ android.view.view$performclick.run(view.java:18770) 01-28 22:27:07.391 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ android.os.handler.handlecallback(handler.java:808) 01-28 22:27:07.391 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ android.os.handler.dispatchmessage(handler.java:103) 01-28 22:27:07.392 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ android.os.looper.loop(looper.java:193) 01-28 22:27:07.392 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ android.app.activitythread.main(activitythread.java:5292) 01-28 22:27:07.392 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ java.lang.reflect.method.invokenative(native method) 01-28 22:27:07.393 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ java.lang.reflect.method.invoke(method.java:515) 01-28 22:27:07.393 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:828) 01-28 22:27:07.393 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:644) 01-28 22:27:07.394 27612-27612/com.example.computer.mathkiddofinal:second w/system.err: @ dalvik.system.nativestart.main(native method) 01-28 22:27:07.394 27612-27612/com.example.computer.mathkiddofinal:second w/dalvikvm: threadid=1: calling uncaughtexceptionhandler 01-28 22:27:07.396 27612-27612/com.example.computer.mathkiddofinal:second e/androidruntime: fatal exception: main process: com.example.computer.mathkiddofinal:second, pid: 27612 java.lang.nullpointerexception @ com.example.computer.mathkiddofinal.grade_level.post_test.grade_four_post_test$1.onclick(grade_four_post_test.java:117) @ android.view.view.performclick(view.java:4463) @ android.view.view$performclick.run(view.java:18770) @ android.os.handler.handlecallback(handler.java:808) @ android.os.handler.dispatchmessage(handler.java:103) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5292) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:828) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:644) @ dalvik.system.nativestart.main(native method) 01-28 22:27:07.400 696-9034/system_process v/provider/settings: settings cache , name = dropbox:data_app_crash , value = null 01-28 22:27:07.401 696-9034/system_process d/dalvikvm: create interp thread : stack size=128kb 01-28 22:27:07.401 696-9034/system_process d/dalvikvm: create new thread 01-28 22:27:07.401 696-9034/system_process d/dalvikvm: new thread created 01-28 22:27:07.401 696-9034/system_process d/dalvikvm: update thread list 01-28 22:27:07.402 696-28496/system_process d/dalvikvm: threadid=79: interp stack @ 0x63b53000 01-28 22:27:07.402 696-28496/system_process d/dalvikvm: threadid=79: created interp 01-28 22:27:07.402 696-9034/system_process d/dalvikvm: start new thread 01-28 22:27:07.402 696-9034/system_process v/provider/settings: settings cache , name = send_action_app_error , value = 1 01-28 22:27:07.402 696-28496/system_process d/dalvikvm: threadid=79: notify debugger 01-28 22:27:07.403 696-9034/system_process w/activitymanager: force finishing activity com.example.computer.mathkiddofinal/.grade_level.post_test.grade_four_post_test
you nullpointerexception this:
radiobutton selectedans = (radiobutton) findviewbyid(rg.getcheckedradiobuttonid()); string selectedanstext = selectedans.gettext().tostring();//nullpointer
because if no radiobutton
selected, findviewbyid
not work, radiobutton
not initialized. should this:
int radiobuttonid = rg.getcheckedradiobuttonid(); if(radiobuttonid!=-1){ //do stuff radiobutton selectedans = (radiobutton) findviewbyid(radiobuttonid); string selectedanstext = selectedans.gettext().tostring(); }else{ toast.maketext(grade_four_post_test.this, "choose answer", toast.length_long).show(); }
Comments
Post a Comment