0

I need to add a radiogroup to an AlertDialog, so i write the follow code:

AlertDialog.Builder screenDialog = new AlertDialog.Builder(this);
screenDialog.setTitle("Captured Screen");

RadioGroup azione = (RadioGroup) findViewById(R.id.azione_stop);

LinearLayout dialogLayout = new LinearLayout(getApplicationContext());
dialogLayout.setOrientation(LinearLayout.VERTICAL);
dialogLayout.addView(azione);
screenDialog.setView(dialogLayout);
screenDialog.show();

And this is the xml containing the RadioGroup:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/azione_stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <RadioButton
        android:id="@+id/ritornare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textColor="#000000"
        android:text="Ritornare" />
    <RadioButton
        android:id="@+id/chiudi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#
        android:text="Chiudi chiamata" />
    <RadioButton
        android:id="@+id/offerta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
    android:text="Segna offerta" />

When I open alertdialog I get an error (java.lang.NullPointerException) where the RadioGroup is added to the LinearLayout. How can i do? Thanks, Mattia

pindol
  • 2,110
  • 6
  • 35
  • 52

1 Answers1

1
RadioGroup azione = (RadioGroup) screenDialog.findViewById(R.id.azione_stop);

and screenDialog.setContentView(R.layout.YOURXML);

AlertDialog screenDialog = new AlertDialog.Builder(this).create();
screenDialog.setTitle("Captured Screen");
screenDialog.setContentView(R.layout.YOURXMLLAYOUT);

RadioGroup azione = (RadioGroup) findViewById(R.id.azione_stop);

screenDialog.show();
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134