android - 当我使用单选按钮时出现空指针异常

标签 android

我想在组按钮中使用 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());  
}

关于android - 当我使用单选按钮时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24108055/

相关文章:

android - 使用 AndEngine 播放动画

android - 实现拖动项目到屏幕边缘功能

php - mysql, ASC 现有mysql查询的数据

android - eclipse 找不到我的 sdk 文件夹

android - 如何在android抽屉导航中添加个人资料信息?

android - 纯android原生隐藏导航栏

android - 签名 apk 后电话验证不起作用

java - 安卓 : openConnection() throws IOExeption ( Unable to find default handler for protocol: http )

Android Studio 3.1 稳定版。构建成功但无法解决错误

android - 如何将广告转换到我的android应用程序?