当我使用单选按钮时,空指针异常



我想使用RadioButton在组按钮和所有在dialog框架,但当我想从我的RadioButton获取文本,我得到一个NullPointerException

这是我的布局XML文件:

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();`

这是我的logCat错误:

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)

你应该这样做:

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());  
}

最新更新