2012-04-16 66 views
0

我需要一個RadioGroup中添加到AlertDialog,所以我寫了如下代碼:添加RadioGroup中到AlertDialog中Android的

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

這是包含RadioGroup中的XML:

<?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" /> 

當我打開alertdialog時,我得到一個錯誤(java.lang.NullPointerException),其中RadioGroup被添加到LinearLayout中。 我該怎麼辦? 謝謝,馬蒂亞

回答

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

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(); 
+0

screenDialog具有不findViewById方法。 – pindol 2012-04-16 13:13:35

+0

你是第一個設置'setContentView'嗎?然後訪問第一行... RadioGroup azione =(RadioGroup)screenDialog.findViewById(R.id.azione_stop); – 2012-04-16 13:15:18

+0

問題是,screenDialog沒有FindViewById和setContentView方法.. – pindol 2012-04-16 13:19:10

相關問題