AndroidRuntimeException:在添加内容之前必须调用requestFeature() [英] AndroidRuntimeException: requestFeature() must be called before adding content

查看:215
本文介绍了AndroidRuntimeException:在添加内容之前必须调用requestFeature()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对话框片段.我打算在活动和对话框中使用此片段.而且我重写了onCreateDialog和onCreateView方法.这是编码.

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

我有两个条件.当屏幕大小正常时,它将调用包括片段对话框的活动.否则,它将调用弹出对话框.当它调用弹出对话框时,我得到了例外.它说 requestFeature()必须在添加内容之前被调用.我可以这样使用吗?我想知道如何克服这个问题.

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.

谢谢.

推荐答案

似乎您的问题与我的问题完全相同-类似代码并引发了相同的异常.

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

解决方案是执行@laalto建议的操作:使用 onCreateView() onCreateDialog(),但不要同时使用它们.我知道异常可能是由许多不同的原因造成的,但这对我来说是有帮助的.希望将来能对其他人有所帮助:)

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

这篇关于AndroidRuntimeException:在添加内容之前必须调用requestFeature()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆