10

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.

user1156041
  • 2,155
  • 5
  • 24
  • 54
  • you should be called first requestFeature before the setcontentView() – Sunil Kumar Jul 05 '13 at 11:36
  • Whether your problem solved?? – Subburaj Jul 06 '13 at 07:26
  • 2
    Likely `setup()` calls `requestFeature()` - you call `setup()` in both in `onCreateView()` and `onCreateDialog()`. (I know, old question but came up in review queues.) – laalto Nov 14 '13 at 11:04
  • from other questions, it could be because you are supposed to call setview before settitle (but that does not work for me, nor does it make any sense) – njzk2 Jan 30 '14 at 19:24
  • I have found at that it does not crash if the onCreateView returns null. Which sucks. – njzk2 Jan 30 '14 at 19:45
  • I'm not sure if this helps you now, but I think this is the best fix http://stackoverflow.com/a/21643675/447549 – Clive Jefferies Feb 17 '14 at 12:14

2 Answers2

20

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 :)

JakeP
  • 1,736
  • 4
  • 23
  • 31
  • what if you need both methods? – SePröbläm Dec 05 '15 at 16:48
  • 1
    I'm not sure that's the right thing to do, according to this: "Implementations should override this class and implement onCreateView(LayoutInflater, ViewGroup, Bundle) to supply the content of the dialog. Alternatively, they can override onCreateDialog(Bundle) to create an entirely custom dialog, such as an AlertDialog, with its own content." (from: http://developer.android.com/reference/android/app/DialogFragment.html). – JakeP Dec 05 '15 at 19:21
  • 1
    The same link also mentions: "Instead of (or in addition to) implementing onCreateView(LayoutInflater, ViewGroup, Bundle) to generate the view hierarchy inside of a dialog, you may implement onCreateDialog(Bundle) to create your own custom Dialog object." But doing so causes the dreaded "AndroidRuntimeException: requestFeature() must be called before adding content" So either the docs or the API is flawed... – SePröbläm Dec 06 '15 at 08:08
  • Thanks @JakeP I solved the issue just leaving the onCreateDialog method, throwing away the onCreateView(). No need to call requestFeature() or something else! – Fuzzo Dec 04 '18 at 09:31
-3

i think you are using requestWindowFeature(); at on create

use it before setContentView(R.layout.activity);

Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27