Reputation: 393
I want to use a RadioButton
in group button and all that in dialog
frame but when i want to get text from my RadioButton
, i get a NullPointerException
.
there is my layout xml file:
LayoutInflater layoutInflater = LayoutInflater.from(context);
View promptView = layoutInflater.inflate(R.layout.prompt_permission, null);
final RadioGroup radioSexGroup = (RadioGroup) promptView.findViewById(R.id.droit);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilder.setView(promptView);
final EditText input = (EditText) promptView.findViewById(R.id.editTextMailUserInput);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//input.setText(input.getText());
int selectedId = radioSexGroup.getCheckedRadioButtonId();
RadioButton radioSexButton =(RadioButton) findViewById(selectedId);
System.out.println(radioSexButton.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();`
this is my logCat error :
06-08 16:10:14.420: E/AndroidRuntime(3645): FATAL EXCEPTION: main
06-08 16:10:14.420: E/AndroidRuntime(3645): java.lang.NullPointerException
06-08 16:10:14.420: E/AndroidRuntime(3645): at com.example.poc_cubbyhole.MainActivity$4.onClick(MainActivity.java:190)
06-08 16:10:14.420: E/AndroidRuntime(3645): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
06-08 16:10:14.420: E/AndroidRuntime(3645): at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 16:10:14.420: E/AndroidRuntime(3645): at android.os.Looper.loop(Looper.java:123)
06-08 16:10:14.420: E/AndroidRuntime(3645): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-08 16:10:14.420: E/AndroidRuntime(3645): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 16:10:14.420: E/AndroidRuntime(3645): at java.lang.reflect.Method.invoke(Method.java:507)
06-08 16:10:14.420: E/AndroidRuntime(3645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-08 16:10:14.420: E/AndroidRuntime(3645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-08 16:10:14.420: E/AndroidRuntime(3645): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 2438
Reputation: 4897
You should do this:
public void onClick(DialogInterface dialog, int id) {
//input.setText(input.getText());
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// change this part like this
RadioButton radioSexButton =(RadioButton) promptView.findViewById(selectedId);
System.out.println(radioSexButton.getText());
}
Upvotes: 1