尝试在空对象引用上调用虚拟方法 'void android.widget.RadioGroup.addView(android.view.View)'

Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View)' on a null object reference

我是 android 的初学者,我使用 java 语言而不是 Kotlin。基本上我在按下按钮后显示一个自定义警报对话框,在警报对话框内有两个单选按钮。但问题是在单击按钮以显示自定义对话框后,它会崩溃并显示空点异常。下面是代码和日志。

CustomDialog.java

public class CustomDialog extends AppCompatDialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater=getActivity().getLayoutInflater();
        View view=layoutInflater.inflate(R.layout.custom_dialog,null);
        builder.setView(view)
                .setTitle("Check Methods");
        final int[] num=getResources().getIntArray(R.array.methods);
        RadioButton radioButton=new RadioButton(getActivity());
        RadioButton radioButton1=new RadioButton(getActivity());
        radioButton.setText("A");
        RadioGroup radioGroup=getActivity().findViewById(R.id.radioGroup);
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int kk=num[0];
                int aa=kk;
            }
        });
        radioButton1.setText("B");
        radioGroup.addView(radioButton); //In this line it is showing null point exception and in debugging it is showing "radioGroup" is null
        radioGroup.addView(radioButton1);
        return builder.create();
    }
}

custom_dialog.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup"/>
</LinearLayout>

methods.xml

<resources>
    <integer-array name="methods">
        <item>0</item>
    </integer-array>
</resources>

MainActivity.Java

btnMethod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDialog(); //to show custom dialog after button press
}}

private void openDialog() {
    CustomDialog customDialog=new CustomDialog();
    customDialog.show(getSupportFragmentManager(),"Custom Dialog");
}

Logs

java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void android.widget.RadioGroup.addView(android.view.View)' 在 com.nabil.myapplication.CustomDialog.onCreateDialog(CustomDialog.java:33) 在 android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330) 在 android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) 在 android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) 在 android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802) 在 android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) 在 android.support.v4.app.FragmentManagerImpl.executeOps 一起 (FragmentManager.java:2411) 在 android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) 在 android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733) 在 android.os.Handler.handleCallback(Handler.java:907) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:216) 在 android.app.ActivityThread.main(ActivityThread.java:7625) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

因为 RadioGroupcustom_dialog.xml 的一个元素,那么你必须从 custom_dialog.xml 的视图中找到 RadioGroup,尝试使用代码:

LayoutInflater layoutInflater=getActivity().getLayoutInflater();
View view=layoutInflater.inflate(R.layout.custom_dialog,null); //load custom_dialog.xml to View
...
RadioGroup radioGroup=view.findViewById(R.id.radioGroup);     //try to retrieve rarioGroup in custom_dialog.xml

更改此行:

 RadioGroup radioGroup=getActivity().findViewById(R.id.radioGroup);

至:

RadioGroup radioGroup = view.findViewById(R.id.radioGroup);

radioGroup 在你的对话框中