【问题标题】:AndroidRuntimeException: requestFeature() must be called before adding content [duplicate]AndroidRuntimeException:在添加内容之前必须调用 requestFeature() [重复]
【发布时间】:2013-07-05 11:33:27
【问题描述】:

我有对话片段。我打算在活动和对话中使用这个片段。我重写了 onCreateDialog 和 onCreateView 方法。这里是编码。

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

我在活动类中就是这样使用这个片段的。

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

我从另一个类似的活动中调用它。

            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() must be called before adding content。我可以这样使用吗?我想知道如何克服这个问题。

谢谢。

【问题讨论】:

  • 你应该在 setcontentView() 之前调用第一个 requestFeature
  • 你的问题解决了吗??
  • 可能setup() 调用requestFeature() - 您在onCreateView()onCreateDialog() 中都调用setup()。 (我知道,老问题,但出现在审核队列中。)
  • 从其他问题来看,可能是因为您应该在 settitle 之前调用 setview (但这对我不起作用,也没有任何意义)
  • 我发现如果 onCreateView 返回 null,它不会崩溃。这很糟糕。

标签: android android-fragments android-dialog android-dialogfragment


【解决方案1】:

看来你的问题和我的完全一样——类似的代码和同样的异常抛出。

解决方案是按照@laalto 的建议:使用onCreateView()onCreateDialog(),但不能同时使用它们。我知道异常可能是由许多不同的事情引起的,但这对我来说是有帮助的;希望它会在未来对其他人有所帮助:)

【讨论】:

  • 如果两种方法都需要怎么办?
  • 我不确定这是正确的做法,根据这个:“实现应该覆盖这个类并实现 onCreateView(LayoutInflater, ViewGroup, Bundle) 来提供对话框的内容。或者,他们可以覆盖 onCreateDialog(Bundle) 来创建一个完全自定义的对话框,例如一个 AlertDialog,它有自己的内容。” (来自:developer.android.com/reference/android/app/DialogFragment.html)。
  • 同一个链接还提到:“代替(或除了)实现 onCreateView(LayoutInflater, ViewGroup, Bundle) 在对话框内生成视图层次结构,您可以实现 onCreateDialog(Bundle)创建您自己的自定义对话框对象。”但是这样做会导致可怕的“AndroidRuntimeException:在添加内容之前必须调用requestFeature()”所以文档或API都有缺陷......
  • 谢谢@JakeP 我解决了这个问题,只是离开了 onCreateDialog 方法,丢弃了 onCreateView()。无需调用 requestFeature() 或其他方法!
【解决方案2】:

我认为您正在使用 requestWindowFeature();在创建时

在setContentView(R.layout.activity);之前使用它

【讨论】:

  • 调用弹出对话框时出现异常。
猜你喜欢
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多