2016-10-27 206 views
0

我有一個警報對話框,有一個5個按鈕的Radiogroup。當按下OK時,應用程序會崩潰,無論我是否選擇單選按鈕。爲Radiogroup嘗試了不同的方法,但都失敗了。如果有人能幫助我,我會很感激。RadioGroup Inside對話框警報崩潰應用程序時。崩潰

PS。我有一個正在調用此警報對話活動的開關盒。 colorpickdialog();

這是我的警告對話框XML:

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radiosizeg" 
    android:layout_below="@+id/bgadddialogsize" 
    android:layoutDirection="rtl" 
    android:gravity="start" 
    android:layout_gravity="start" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true"> 
<RadioButton 
    android:text="@string/sizesmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/sizesmall"/> 

<RadioButton 
    android:text="@string/sizemed" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/sizemed" /> 

    <RadioButton 
     android:text="@string/sizelar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/sizelar" /> 

    <RadioButton 
     android:text="@string/sizefill" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/sizefill" /> 

    <RadioButton 
     android:text="@string/sizecustom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/sizecustom" /> 
</RadioGroup> 

,這是我MainActivity.class:

public void colorpickdialog() { 
    // get prompts.xml view 
    final RadioGroup radiosizeg = (RadioGroup) findViewById(R.id.radiosizeg); 
    LayoutInflater layoutInflater2 = LayoutInflater.from(MainActivity.this); 
    View promptView2 = layoutInflater2.inflate(R.layout.colorpickdialog, null); 
    AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(MainActivity.this); 
    alertDialogBuilder2.setView(promptView2); 
    // setup a dialog window 
    alertDialogBuilder2.setCancelable(false) 
      .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        radiosizeg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(RadioGroup radioGroup, int i) { 
          int id = radiosizeg.getCheckedRadioButtonId(); 
          switch (id) { 
           case R.id.sizesmall: 
            Toast.makeText(getApplicationContext(),"A", Toast.LENGTH_SHORT).show(); 
            break; 
           case R.id.sizemed: 
            Toast.makeText(getApplicationContext(),"B", Toast.LENGTH_SHORT).show(); 
            break; 
           case R.id.sizelar: 
            Toast.makeText(getApplicationContext(),"C", Toast.LENGTH_SHORT).show(); 
            break; 
           case R.id.sizefill: 
            Toast.makeText(getApplicationContext(),"D", Toast.LENGTH_SHORT).show(); 
            break; 
           case R.id.sizecustom: 
            Toast.makeText(getApplicationContext(),"E", Toast.LENGTH_SHORT).show(); 
            break; 
           default: 
            onCheckedChanged(radioGroup, i); 
            break; 
          } 
         } 
        }); 
       } 
      }) 
      .setNegativeButton(getString(R.string.cancel), 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 

錯誤堆棧跟蹤

10-27 20:56:53.753 4312-4327/? E/ReportTools: This is not beta user build 
10-27 20:56:53.959 4312-7462/? E/HsmCoreServiceImpl: onTransact in code is: 102 
10-27 20:56:54.025 13521-13539/? E/HwLauncher: SettingsEx , no such field. 
10-27 20:56:54.088 8152-8242/? E/PackageLogInfoManager: checkPackageLogState, cr: [email protected], packageNames: null 
+0

請提交錯誤日誌 –

+0

這不是錯誤日誌。注意沒有'? E /'。您只有信息和警告消息。 –

+0

此日誌是所有應用程序的統稱。過濾日誌並再次提交。如果使用android studio。你可以看到Android監視器右側的過濾器選項 – Shuddh

回答

0

我想通了sulotion。

的問題是,在RadioGroup中在實際視圖中定義:

final RadioGroup radiosizeg = (RadioGroup) findViewById(R.id.radiosizeg); 

但是,我已膨脹的觀點爲:

LayoutInflater layoutInflater2 = LayoutInflater.from(MainActivity.this); 
    View promptView2 = layoutInflater2.inflate(R.layout.colorpickdialog, null); 
    AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(MainActivity.this); 
    alertDialogBuilder2.setView(promptView2); 

因此,解決方案是內部定義視圖promptView2:

final RadioGroup radiosizeg = (RadioGroup) promptView2.findViewById(R.id.radiosizeg) 

很好();