Problem inflating custom view for AlertDialog in DialogFragment Problem inflating custom view for AlertDialog in DialogFragment android android

Problem inflating custom view for AlertDialog in DialogFragment


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.

@Overridepublic 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();}


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 beintegrated elsewhere.
  2. Override onCreateView only - Does not work as a dialog but can beintegrated elsewhere.
  3. Override both - Works as a dialog and can be integratedelsewhere.

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.


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