Problem inflating custom view for AlertDialog in DialogFragment

Solution 1:

The first error line gives me the hint that this is related to how you are creating your dialog - not the dialog itself.

Are you creating the dialog automatically (which could mean this gets called before the views are all set up) or in response to a button click? I initially had problems with fragments due to instantiation order.

I used the same code to set the view as you have, and my result works. I cut out the other setup to make this look cleaner, but it works with or without it.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null);
    builder.setView(view);

    return builder.create();
}

Solution 2:

I'm surprised by these answers as none of them solve the problem.

A DialogFragment allows you to reuse the same UI for both a dialog and integrated in your app elsewhere as a fragment. Quite a useful feature. As per google's documentation, you can achieve this by overriding onCreateDialog and onCreateView. http://developer.android.com/reference/android/app/DialogFragment.html

There are three scenarios here:

  1. Override onCreateDialog only - Works as a dialog but cannot be integrated elsewhere.
  2. Override onCreateView only - Does not work as a dialog but can be integrated elsewhere.
  3. Override both - Works as a dialog and can be integrated elsewhere.

Solution: The AlertDialog class is calling another class which calls requestFeature. To fix this.. Don't use the AlertDialog, instead use a plain Dialog or whatever super.onCreateDialog returns. This the solution that I have found works best.

Caveat: Other dialogs such as DatePickerDialog, ProgressDialog, TimePickerDialog all inherit from AlertDialog and will likely cause the same error.

Bottom Line: DialogFragment is good if you need to create very customized interface that needs to be used in several places. It doesn't appear to work to reuse existing android dialogs.

Solution 3:

Avoid request feature crash and use same layout:

public class MyCombinedFragment extends DialogFragment
{
    private boolean isModal = false;

    public static MyCombinedFragment newInstance()
    {
        MyCombinedFragment frag = new MyCombinedFragment();
        frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
        return frag;
    }

    public MyCombinedFragment()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        if(isModal) // AVOID REQUEST FEATURE CRASH
        {
        return super.onCreateView(inflater, container, savedInstanceState);
        }
        else
        {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        setupUI(view);
        return view;
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder alertDialogBuilder = null;
        alertDialogBuilder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_layout, null);
        alertDialogBuilder.setView(view);
        alertDialogBuilder.setTitle(“Modal Dialog“);
        alertDialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        setupUI(view);
        return alertDialogBuilder.create();
    }
}

Solution 4:

I had the same problem. In my case it was becasue Android Studio created a template onCreateView that re-inflated a new view instead of returning the view created in onCreateDialog. onCreateView is called after onCreateDialog, so the solution was to simply reurnt the fragments view.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return this.getView();
}

Solution 5:

Faced the same issue, and it took lot of time to get rid of the error. Finally passing resource ID to setView() method solved the problem. Add set view as below:

.setView(R.layout.dialog)