我想在RadioButton
框架中使用dialog
和RadioButton
框架中的所有内容,但当我想从NullPointerException
获取文字时,我会得到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();`
。
我的布局xml文件:
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)
这是我的logCat错误:
{{1}}
答案 0 :(得分:1)
你应该这样做:
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());
}