android - RadioGroup retrieve is null -
i try retrieve radiogroup check radio button checked. this, use code in oncreate() :
/** * @param savedinstancestate */ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); radiogroup_langue = (radiogroup) findviewbyid(r.id.radiogroup_langue); radiogroup_mode = (radiogroup) findviewbyid(r.id.rgroup_modeconnexion); ... }
but use debug, , androidstudio tell me radiogroup_langue null. nullpointer exception.
in alertdialog, when user click on ok button :
.setnegativebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // if button clicked, close // dialog box , nothing radiogroup_langue.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { public void oncheckedchanged(radiogroup radiogroup_langue, int checkedid) { // radiobutton has changed in check state radiobutton checkedradiobutton = (radiobutton)radiogroup_langue.findviewbyid(checkedid); // puts value (true/false) variable boolean ischecked = checkedradiobutton.ischecked(); /// if radiobutton has changed in check state checked... if (ischecked) { // changes textview's text "checked: example radiobutton text" toast.maketext(mainactivity.this,"checked:" + checkedradiobutton.gettext(),toast.length_long).show(); } } }); }});
my xml :
<radiogroup android:layout_width="148dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:id="@+id/radiogroup_langue"> <radiobutton android:layout_width="137dp" android:layout_height="wrap_content" android:text="@string/langue_1" android:id="@+id/bouttonradio_en" android:layout_gravity="center_horizontal" android:textsize="20dp" android:textstyle="bold" /> <radiobutton android:layout_width="137dp" android:layout_height="wrap_content" android:text="@string/langue_2" android:id="@+id/bouttonradio_fr" android:layout_gravity="center_horizontal" android:textsize="20dp" android:textstyle="bold" /> </radiogroup>
error log :
06-01 22:51:53.053 21336-21336/com.example.my020571.sterela2 e/androidruntime: fatal exception: main process: com.example.my020571.sterela2, pid: 21336 java.lang.nullpointerexception @ com.example.my020571.sterela2.mainactivity$4.onclick(mainactivity.java:321) @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:170) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:146) @ android.app.activitythread.main(activitythread.java:5598) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) @ dalvik.system.nativestart.main(native method)
line 321 :
int selectedid = rgroup_langue.getcheckedradiobuttonid();
method :
private void changerlangue() { alertdialog.builder alertdialogbuilder = new alertdialog.builder( context); final view view = view.inflate(mainactivity.this, r.layout.changer_langue, null); alertdialogbuilder.setview(view); // titre de la fenĂȘtre alertdialogbuilder.settitle("langue"); // set dialog message alertdialogbuilder .setmessage("veuillez choisir votre langue :") .setcancelable(false) .seticon(r.drawable.logo_langue) .setpositivebutton("annuler", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // if button clicked, close // current activity dialog.cancel(); } }) .setnegativebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // if button clicked, close // dialog box , nothing int selectedid = rgroup_langue.getcheckedradiobuttonid(); radiobutton selectedradiobutton = (radiobutton)findviewbyid(selectedid); toast.maketext(getapplicationcontext(),selectedradiobutton.gettext().tostring(),toast.length_short).show(); }}); // create alert dialog alertdialog alertdialog = alertdialogbuilder.create(); // show alertdialog.show(); }
all of views going null without setcontentview
in oncreate
.
private void radiogroup radiogroup_langue; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.yourlayoutfile); // todo: replace file radiogroup_langue = (radiogroup) findviewbyid(r.id.radiogroup_langue); }
another reason null if don't use xml file contains @+id/radiogroup_langue
(and other ids want find) in setcontentview
.
edit
since using alertdialog show view, need use findviewbyid
on inflated view instead of on activity.
alertdialog.builder alertdialogbuilder = new alertdialog.builder(mainactivity.this); // view dialog final view view = view.inflate(mainactivity.this, r.layout.changer_langue, null); // find views within rgroup_langue = (radiogroup) view.findviewbyid(r.id.radiogroup_langue); rgroup_langue.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { public void oncheckedchanged(radiogroup group, int checkedid) { radiobutton checkedradiobutton = (radiobutton) group.findviewbyid(checkedid); // todo: implement }}); // build dialog alertdialog alertdialog = alertdialogbuilder .setview(view) // load view .settitle("langue") .setmessage("veuillez choisir votre langue") ... .setnegativebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // need use findviewbyid radiogroup here int selectedid = rgroup_langue.getcheckedradiobuttonid(); string msg = null; if (selectedid != -1) { radiobutton selectedradiobutton = (radiobutton)rgroup_langue.findviewbyid(selectedid); msg = selectedradiobutton.gettext().tostring(); } else { msg = "nothing selected"; } toast.maketext(getapplicationcontext(),msg,toast.length_short).show(); }}) .create(); // show alertdialog.show();
sidenote: don't think setmessage
, setview
can used @ same time.