90

我正在尝试使用我自己的布局创建一个 DialogFragment。

我见过几种不同的方法。有时布局是在 OnCreateDialog 中设置的,如下所示:(我使用的是 Mono,但我已经有点习惯 Java)

public override Android.App.Dialog OnCreateDialog (Bundle savedInstanceState)
{
    base.OnCreateDialog(savedInstanceState);
    AlertDialog.Builder b = new AlertDialog.Builder(Activity);
        //blah blah blah
    LayoutInflater i = Activity.LayoutInflater;
    b.SetView(i.Inflate(Resource.Layout.frag_SelectCase, null));
    return b.Create();
}

第一种方法对我有用......直到我想使用findViewByID. 所以经过一番谷歌搜索后,我尝试了第二种方法,它涉及覆盖OnCreateView

所以我注释掉了OnCreateDialog设置布局的两行,然后添加了这个:

public override Android.Views.View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.Inflate(Resource.Layout.frag_SelectCase, container, false);
        //should be able to use FindViewByID here...
    return v;
}

这给了我一个可爱的错误:

11-05 22:00:05.381: E/AndroidRuntime(342): FATAL EXCEPTION: main
11-05 22:00:05.381: E/AndroidRuntime(342): android.util.AndroidRuntimeException: requestFeature() must be called before adding content

我难住了。

4

6 回答 6

57

我对以下代码有同样的例外:

public class SelectWeekDayFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
        .setMessage("Are you sure?").setPositiveButton("Ok", null)
        .setNegativeButton("No way", null).create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.week_day_dialog, container, false);

        return view;    
    }
}

您必须选择覆盖DialogFragment 中的 onCreateView 或 onCreateDialog 之一。覆盖两者将导致异常:“在添加内容之前必须调用requestFeature()”。

重要的

如需完整答案,请查看@TravisChristian 评论。正如他所说,您确实可以覆盖两者,但是当您在创建对话框视图后尝试膨胀视图时,问题就来了。

于 2013-03-24T19:00:10.023 回答
38

第一种方法对我有用……直到我想使用 FindViewByID。

我猜你的范围不是findViewById()由返回的视图inflate(),试试这个:

View view = i.inflate(Resource.Layout.frag_SelectCase, null);
// Now use view.findViewById() to do what you want
b.setView(view);

return b.create();
于 2012-11-06T18:33:18.523 回答
36

下面的代码来自谷歌指南,所以答案是你不能像你在 onCreateDialog() 中那样做,你必须使用 super.onCreateDialog() 来获得一个对话框。

public class CustomDialogFragment extends DialogFragment {
    /** The system calls this to get the DialogFragment's layout, regardless
        of whether it's being displayed as a dialog or an embedded fragment. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.purchase_items, container, false);
    }

    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
}
于 2013-10-10T18:04:12.743 回答
18

这是在对话框片段中使用 findViewById 的示例

public class NotesDialog extends DialogFragment {

        private ListView mNotes;
       private RelativeLayout addNote;

        public NotesDialog() {
            // Empty constructor required for DialogFragment
        }



        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            View view = getActivity().getLayoutInflater().inflate(R.layout.note_dialog, null);
            mNotes = (ListView) view.findViewById(R.id.listViewNotes);
            addNote = (RelativeLayout) view.findViewById(R.id.notesAdd);

            addNote.setOnClickListener(new View.OnClickListener(){
                 @Override
                 public void onClick(View v){


                     getDialog().dismiss();

                     showNoteDialog();
                 }
             });

            builder.setView(view);

            builder.setTitle(bandString);


            builder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                          getDialog().dismiss();
                        }
                    }
                );


           return  builder.create();


    }
于 2014-08-31T01:32:16.720 回答
11

正如@Xavier Egea 所说,如果您同时实现了 onCreateView() 和 onCreateDialog() ,则可能会出现“必须在添加内容之前调用 requestFeature()”崩溃的风险。这是因为当您将该片段作为对话框显示()时,会同时调用 onCreateDialog() 和 onCreateView()(为什么,我不知道)。正如 Travis Christian 提到的,在 onCreateDialog() 中创建对话框后 onCreateView() 中的 inflate() 是导致崩溃的原因。

实现这两个功能但避免这种崩溃的一种方法:使用 getShowsDialog() 来限制 onCreateView() 的执行(因此不会调用您的 inflate() )。这样,当您将 DialogFragment 显示为对话框时,只会执行您的 onCreateDialog() 代码,但是当您的 DialogFragment 用作布局中的片段时,可以调用您的 onCreateView() 代码。

// Note: if already have onCreateDialog() and you only ever use this fragment as a 
// dialog, onCreateView() isn't necessary
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (getShowsDialog() == true) {  // **The key check**
        return super.onCreateView(inflater, container, savedInstanceState);
    } else {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_alarm_dialog, null);    
        return configureDialogView(view);
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{ 
    // Return custom dialog...
    Dialog dialog = super.onCreateDialog(savedInstanceState); // "new Dialog()" will cause crash

    View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_alarm_dialog, null);    
    configureDialogView(view);
    dialog.setContentView(view);

    return dialog;
}

// Code that can be reused in both onCreateDialog() and onCreateView()
private View configureDialogView(View v) {      
    TextView myText = (TextView)v.findViewById(R.id.myTextView);
    myText.setText("Some Text");

    // etc....

    return v;
}
于 2014-11-23T00:42:44.813 回答
7

如果您想轻松访问对话框属性,例如标题和关闭按钮,但还想使用自己的布局,则可以在覆盖 onCreateDialog 时将 LayoutInflator 与 Builder 一起使用。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Message!")
        .setTitle(this.dialogTitle)
        .setView(inflater.inflate(R.layout.numpad_dialog, null))
        .setPositiveButton(R.string.enter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Clicked 'Okay'
            }
        })
        .setNegativeButton(R.string.dismiss, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Clicked 'Cancel'
            }
        });
    return builder.create();
}
于 2014-04-08T05:37:00.527 回答