android - Error while retrieving values of selected RADIO buttons -
i trying values of selected radiobutton on click of submit button, getting nullpointer exception @ line :
string selection = (string) btn.gettext();
error :
05-12 12:54:52.261: e/androidruntime(14113): fatal exception: main 05-12 12:54:52.261: e/androidruntime(14113): process: com.androidhub4you.multilevellistview, pid: 14113 05-12 12:54:52.261: e/androidruntime(14113): java.lang.nullpointerexception 05-12 12:54:52.261: e/androidruntime(14113): @ com.androidhub4you.multilevellistview.mainactivity$5.onclick(mainactivity.java:249) 05-12 12:54:52.261: e/androidruntime(14113): @ android.view.view.performclick(view.java:4456) 05-12 12:54:52.261: e/androidruntime(14113): @ android.view.view$performclick.run(view.java:18465) 05-12 12:54:52.261: e/androidruntime(14113): @ android.os.handler.handlecallback(handler.java:733) 05-12 12:54:52.261: e/androidruntime(14113): @ android.os.handler.dispatchmessage(handler.java:95) 05-12 12:54:52.261: e/androidruntime(14113): @ android.os.looper.loop(looper.java:136) 05-12 12:54:52.261: e/androidruntime(14113): @ android.app.activitythread.main(activitythread.java:5086) 05-12 12:54:52.261: e/androidruntime(14113): @ java.lang.reflect.method.invokenative(native method) 05-12 12:54:52.261: e/androidruntime(14113): @ java.lang.reflect.method.invoke(method.java:515) 05-12 12:54:52.261: e/androidruntime(14113): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) 05-12 12:54:52.261: e/androidruntime(14113): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 05-12 12:54:52.261: e/androidruntime(14113): @ dalvik.system.nativestart.main(native method)
submit button code :
submitbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(radiogroup.getcheckedradiobuttonid()!=-1){ string selection = btn.gettext().tostring(); toast.maketext(getapplicationcontext(), "you submitting : " + selection, toast.length_long).show(); } } });
full code :
public class mainactivity extends activity { private arraylist<product>pproductarraylist; private arraylist<subcategory>psubitemarraylist; private arraylist<subcategory>psubitemarraylist2; private linearlayout mlinearlistview; boolean isfirstviewclick=false; boolean issecondviewclick=false; radiobutton rdbtn; private radiogroup radiogroup; private radiobutton btn; private button submitbutton; private string[] shownames = {"aa", "bb", "cc"}; //dynamic show names private string[] application = {"dd", "ee", "ff", "gg"}; //dynamic application names private string[] device = {"hh", "ii", "jj", "kk"}; //dynamic device names private string text1; private string text2; private layoutinflater inflater3; private layoutinflater inflater4; private view mlinearview3; private view mlinearview4; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mlinearlistview = (linearlayout) findviewbyid(r.id.linear_listview); submitbutton = (button)findviewbyid(r.id.submitbutton); arraylist<itemlist> mitemlistarray=new arraylist<itemlist>(); mitemlistarray.add(new itemlist("", "")); arraylist<itemlist> mitemlistarray2=new arraylist<itemlist>(); mitemlistarray2.add(new itemlist("", "")); psubitemarraylist=new arraylist<subcategory>(); psubitemarraylist2=new arraylist<subcategory>(); psubitemarraylist.add(new subcategory("select application/device", mitemlistarray)); //static header psubitemarraylist2.add(new subcategory("select device", mitemlistarray2)); //static header pproductarraylist=new arraylist<product>(); (int = 0; < shownames.length; i++){ pproductarraylist.add(new product(shownames[i], psubitemarraylist)); } (int = 0; < pproductarraylist.size(); i++) { layoutinflater inflater = null; inflater = (layoutinflater) getapplicationcontext().getsystemservice(context.layout_inflater_service); view mlinearview = inflater.inflate(r.layout.row_first, null); final textview mproductname = (textview) mlinearview.findviewbyid(r.id.textviewname); final relativelayout mlinearfirstarrow=(relativelayout)mlinearview.findviewbyid(r.id.linearfirst); final imageview mimagearrowfirst=(imageview)mlinearview.findviewbyid(r.id.imagefirstarrow); final linearlayout mlinearscrollsecond=(linearlayout)mlinearview.findviewbyid(r.id.linear_scroll); if(isfirstviewclick==false){ mlinearscrollsecond.setvisibility(view.gone); mimagearrowfirst.setbackgroundresource(r.drawable.arw_lt); } else{ mlinearscrollsecond.setvisibility(view.visible); mimagearrowfirst.setbackgroundresource(r.drawable.arw_down); } mlinearfirstarrow.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if(isfirstviewclick==false){ isfirstviewclick=true; mimagearrowfirst.setbackgroundresource(r.drawable.arw_down); mlinearscrollsecond.setvisibility(view.visible); }else{ isfirstviewclick=false; mimagearrowfirst.setbackgroundresource(r.drawable.arw_lt); mlinearscrollsecond.setvisibility(view.gone); } return false; } }); final string name = pproductarraylist.get(i).getpname(); mproductname.settext(name); /** * */ (int j = 0; j < pproductarraylist.get(i).getmsubcategorylist().size(); j++) { layoutinflater inflater2 = null; inflater2 = (layoutinflater) getapplicationcontext().getsystemservice(context.layout_inflater_service); view mlinearview2 = inflater2.inflate(r.layout.row_second, null); textview msubitemname = (textview) mlinearview2.findviewbyid(r.id.textviewtitle); final relativelayout mlinearsecondarrow=(relativelayout)mlinearview2.findviewbyid(r.id.linearsecond); final imageview mimagearrowsecond=(imageview)mlinearview2.findviewbyid(r.id.imagesecondarrow); final linearlayout mlinearscrollthird=(linearlayout)mlinearview2.findviewbyid(r.id.linear_scroll_third); if(issecondviewclick==false){ mlinearscrollthird.setvisibility(view.gone); mimagearrowsecond.setbackgroundresource(r.drawable.arw_lt); } else{ mlinearscrollthird.setvisibility(view.visible); mimagearrowsecond.setbackgroundresource(r.drawable.arw_down); } mlinearsecondarrow.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if(issecondviewclick==false){ issecondviewclick=true; mimagearrowsecond.setbackgroundresource(r.drawable.arw_down); mlinearscrollthird.setvisibility(view.visible); }else{ issecondviewclick=false; mimagearrowsecond.setbackgroundresource(r.drawable.arw_lt); mlinearscrollthird.setvisibility(view.gone); } return false; } }); final string catname = pproductarraylist.get(i).getmsubcategorylist().get(j).getpsubcatname(); msubitemname.settext(catname); (int k = 0; k < pproductarraylist.get(i).getmsubcategorylist().get(j).getmitemlistarray().size(); k++) { inflater3 = null; inflater3 = (layoutinflater) getapplicationcontext().getsystemservice(context.layout_inflater_service); mlinearview3 = inflater3.inflate(r.layout.row_third, null); radiogroup = (radiogroup) mlinearview3.findviewbyid(r.id.myradiogroup); for(int m=0;m<application.length;m++) { rdbtn = new radiobutton(this); rdbtn.setid(m); rdbtn.settext(application[m]); radiogroup.addview(rdbtn); } radiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { public void oncheckedchanged(radiogroup mradiogroup, int checkedid) { for(int i=0; i<mradiogroup.getchildcount(); i++) { btn = (radiobutton) mradiogroup.getchildat(i); int t=mradiogroup.getid(); system.out.println(t); if(btn.getid() == checkedid) { text1 = btn.gettext().tostring(); toast.maketext(getapplicationcontext(), "you selected : " + text1 + " : " + mproductname.gettext().tostring(), toast.length_short).show(); return; } } } }); mlinearscrollthird.addview(mlinearview3); } (int l = 0; l < pproductarraylist.get(i).getmsubcategorylist().get(j).getmitemlistarray().size(); l++) { inflater4 = null; inflater4 = (layoutinflater) getapplicationcontext().getsystemservice(context.layout_inflater_service); mlinearview4 = inflater4.inflate(r.layout.row_third, null); radiogroup = (radiogroup) mlinearview4.findviewbyid(r.id.myradiogroup); for(int m=0;m<device.length;m++) { rdbtn = new radiobutton(this); rdbtn.setid(m); rdbtn.settext(device[m]); radiogroup.addview(rdbtn); } radiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { public void oncheckedchanged(radiogroup mradiogroup, int checkedid) { for(int i=0; i<mradiogroup.getchildcount(); i++) { btn = (radiobutton) mradiogroup.getchildat(i); int t=mradiogroup.getid(); system.out.println(t); if(btn.getid() == checkedid) { text2 = btn.gettext().tostring(); toast.maketext(getapplicationcontext(), "you selected : " + text2 + " : " + mproductname.gettext().tostring(), toast.length_short).show(); return; } } } }); mlinearscrollthird.addview(mlinearview4); } mlinearscrollsecond.addview(mlinearview2); } mlinearlistview.addview(mlinearview); } submitbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(radiogroup.getcheckedradiobuttonid()!=-1){ string selection = btn.gettext().tostring(); toast.maketext(getapplicationcontext(), "you submitting : " + selection, toast.length_long).show(); } } }); } }
now try this: selection = btn.gettext().tostring();
Comments
Post a Comment