英文:
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("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)
答案1
得分: 1
因为RadioGroup
是custom_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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论