AndroidRuntimeException: requestFeature() must be called before adding content

user1156041 picture user1156041 · Jul 5, 2013 · Viewed 8.3k times · Source

I have dialog fragment. I have intention to use this fragment in activity and dialog. And I override onCreateDialog and onCreateView method. here is coding.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.interval_time_popup, null);
        setup(view, false);
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.interval_time_popup, null);

        builder.setTitle("Interval Time");
        builder.setView(view);
        setup(view, true);
        builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                listener.setOnIntervalTime(hourNp.getValue(), minNp.getValue());
                dismiss();
            }
        });
        builder.setNegativeButton("Cancel", new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dismiss();
            }
        });
        return builder.create();
    }

I use this fragment in the activity class like that.

           SelectTimeIntervalDialogFragment fragment = new SelectTimeIntervalDialogFragment();
            fragment.setHrMin(hr, min);
            Bundle args = new Bundle();

            FragmentTransaction t = getSupportFragmentManager().beginTransaction();
            t.replace(R.id.shift_period_interval_layout, fragment);
            t.commit();

I call it from another activity like that.

            if((getResources().getConfiguration().screenLayout &
                            Configuration.SCREENLAYOUT_SIZE_NORMAL) ==
                            Configuration.SCREENLAYOUT_SIZE_NORMAL){
                        Intent intent = new Intent(ShiftPeriodActivity.this, SelectIntervalActivity.class);
                        intent.putExtra("intervalHr", speriod.intervalHr);
                        intent.putExtra("intervalMin", speriod.intervalMin);
                        startActivityForResult(intent, 1);
                    } else {
                        FragmentManager fm = getSupportFragmentManager();
                        intervalDialog = new SelectTimeIntervalDialogFragment();
                        intervalDialog.setHrMin(speriod.intervalHr, speriod.intervalMin);
                        intervalDialog.show(fm, "interval_fragment");
                    }

I have two conditions. When the screen size is normal, it call activity which include fragment dialog. Otherwise, it call the popup dialog. I got the exception when it call the popup dialog. It said requestFeature() must be called before adding content. Can I use like this? I would like to know how to overcome this.

Thanks.

Answer

JakeP picture JakeP · Feb 12, 2014

It seems your problem is exactly the same as mine - similar-ish code and the same exception thrown.

The solution is to do what @laalto suggested: use either onCreateView() or onCreateDialog() but not both of them at the same time. I know the exception can be seemingly caused by many different things but this is what helped in my case; hopefully it'll help someone else in the future :)