2014-04-02 163 views
0

我正在嘗試爲我的AlertDialog應用自定義視圖。我的問題是,我正在使用fragment s,一個listview與一個自定義適配器,我的alertdialog和我認爲它得到ViewGroup s混合起來。AlertDialog的自定義視圖

這裏就是我有我的alertdialog,但它總是崩潰給我一個

UnsupportedOperationException異常addview在適配器視圖不支持

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if(item.getItemId() == R.id.action_add) { 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     View dialoglayout = inflater.inflate(R.layout.dialog_new_site, (ViewGroup)getActivity().getCurrentFocus()); 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 
     alertDialogBuilder.setView(dialoglayout); 
+0

試試這個'查看dialoglayout = inflater.inflate(R.layout.dialog_new_site,NULL);' – upenpat

+0

謝謝!我不知道你可以給它ViewGroup –

回答

0

試試這個:

View dialoglayout = getActivity().getLayoutInflater().inflate(R.layout.dialog_new_site, null); 
0

嘗試這樣做

// Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    view v = inflater.inflate(R.layout.dialog_layout, null); 
0

嘗試創建自定義警報對話框並使用它。

public class MyAlertDialog extends Dialog { 

    Context mContext; 

    public InfoCustomDialog(Context context, int theme) { 
    super(context, theme); 
    this.mContext = context; 
    } 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
      setContentView(R.layout.dialog_new_site); 
    } 
} 

然後用

MyAlertDialog alertDialog = new MyAlertDialog(getActivity()); 
    alertDialog.show(); 
相關問題