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

huangapple go评论73阅读模式
英文:

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

问题

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: Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View)' on a null object reference
    at com.nabil.myapplication.CustomDialog.onCreateDialog(CustomDialog.java:33)
    at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
    at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
英文:

I am a beginner in android and i using java language not Kotlin. Basically i am showing a custom alert dialog after button press, inside an alert dialog there is two radio buttons. But problem is after clicking a button in order to show the custom dialog it gets crashed showing null point exception. Below is the code and log.

> 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(&quot;Check Methods&quot;);
        final int[] num=getResources().getIntArray(R.array.methods);
        RadioButton radioButton=new RadioButton(getActivity());
        RadioButton radioButton1=new RadioButton(getActivity());
        radioButton.setText(&quot;A&quot;);
        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(&quot;B&quot;);
        radioGroup.addView(radioButton); //In this line it is showing null point exception and in debugging it is showing &quot;radioGroup&quot; is null
        radioGroup.addView(radioButton1);
        return builder.create();
    }
}

> custom_dialog.xml

&lt;LinearLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;
    &lt;RadioGroup
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/radioGroup&quot;/&gt;
&lt;/LinearLayout&gt;

> methods.xml

&lt;resources&gt;
    &lt;integer-array name=&quot;methods&quot;&gt;
        &lt;item&gt;0&lt;/item&gt;
    &lt;/integer-array&gt;
&lt;/resources&gt;

> 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(),&quot;Custom Dialog&quot;);
}

> Logs

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View)' on a null object reference
at com.nabil.myapplication.CustomDialog.onCreateDialog(CustomDialog.java:33)
at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

答案1

得分: 1

因为RadioGroupcustom_dialog.xml的一个元素,所以你必须从custom_dialog.xml的视图中找到RadioGroup,可以尝试以下代码:

LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.custom_dialog, null); // 将custom_dialog.xml加载到View中
...
RadioGroup radioGroup = view.findViewById(R.id.radioGroup); // 尝试在custom_dialog.xml中检索radioGroup

请注意,以上内容是翻译后的结果,只包含代码部分的翻译。

英文:

Because RadioGroup is an element of the custom_dialog.xml then you must find RadioGroup from View of custom_dialog.xml, try with the code:

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

答案2

得分: 1

将这一行:

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

改为:

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

radioGroup 在你的对话框中。

英文:

Change this line:

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

to:

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

radioGroup is in your dialog

huangapple
  • 本文由 发表于 2020年10月4日 15:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64192228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定