当单击 Alertdialog postiveButton 以获取在 Alertdialog 中弹出的单选按钮文本时,应用程序停止

App stops when Alertdialog postiveButton is clicked to get the radioButton Text which pops up in a Alertdialog

当我单击布局时,会弹出一个带有两个单选按钮的警告对话框。我想要的是获取 radioButton 文本并在我单击“确定”按钮时将该文本显示到 Toast 中。但在单击确定按钮后,应用程序停止工作。为什么?

Methodlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    AlertDialog.Builder builder=new AlertDialog.Builder(Add_AlarmActivity.this);
                    View v=getLayoutInflater().inflate(R.layout.method_pop,null);

                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            int selectedId=radioGroup.getCheckedRadioButtonId();
                            radioButton=(RadioButton) findViewById(selectedId);
                            String s=radioButton.getText().toString();
                            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });

                    builder.setView(v);
                    AlertDialog dialog=builder.create();
                    dialog.show();
                }
            });

这里是 xml 文件

 <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <RadioGroup
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/method_radio"
                    android:gravity="center_horizontal"
                    android:layout_margin="10dp">
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Solve math"
                        android:id="@+id/solve_math"
                        android:checked="false"/>
                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Shake"
                        android:id="@+id/shake"
                        android:checked="false"
                        android:layout_marginRight="15dp"/>

                </RadioGroup>
            </RelativeLayout>

        </RelativeLayout>

[这是日志][1]:https://i.stack.imgur.com/4vgG6.png

01-24 17:40:27.466 24560-24560/com.example.application.alarta E/AndroidRuntime:致命异常:main 进程:com.example.application.alarta,PID:24560 java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'int android.widget.RadioGroup.getCheckedRadioButtonId()' 在 com.example.application.alarta.Add_AlarmActivity$3$1.onClick(Add_AlarmActivity.java:175) 在 com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

好的,您没有初始化 RadioGroup

应该在View v充气后初始化

Methodlayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        AlertDialog.Builder builder=new AlertDialog.Builder(Add_AlarmActivity.this);
        View v=getLayoutInflater().inflate(R.layout.method_pop,null);

        final RadioGroup radioGroup = (RadioGroup) v.findViewById(R.id.method_radio);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                int selectedId=radioGroup.getCheckedRadioButtonId();
                RadioButton radioButton=(RadioButton) v.findViewById(selectedId);
                String s=radioButton.getText().toString();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });

        builder.setView(v);
        AlertDialog dialog=builder.create();
        dialog.show();
    }
});

我没有 运行 代码,因此可能需要更改。