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)
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