显示DialogFragment时,“添加内容之前必须先调用android.util.AndroidRuntimeException:requestFeature()”

时间:2019-05-13 12:42:49

标签: android android-dialogfragment fragmentmanager dialogfragment

我尝试了所有解决方案,但似乎没有一个对我有用-.-

我的手机运行的是Android 5.0.2版本,这就是我要尝试的做法。

我想从我的活动中打开一个对话框片段,如下所示:

 SiteRadiusDialog dialog = SiteRadiusDialog.newInstance();
            dialog.setRadiusListener(this);
            dialog.show(getSupportFragmentManager(), "RadiusDialog");

这是我的SiteRadiusDialog:

public class SiteRadiusDialog extends android.support.v4.app.DialogFragment {

private SeekBar radiusSeekBar;
private TextView tvProgress;
private Button minus;
private Button plus;
private SiteRadiusChangeListener listener;
private int radius;
private int finalProgress;

public static SiteRadiusDialog newInstance() {
    return new SiteRadiusDialog();
}

public void setRadiusListener(SiteRadiusChangeListener listener) {
    this.listener = listener;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    return inflater.inflate(R.layout.dialog_site_radius, container);
}


@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context context = getActivity();

    LayoutInflater inflater = LayoutInflater.from(context);

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.radius_change_title);
    View dialogView = inflater.inflate(R.layout.dialog_site_radius, null);
    builder.setView(dialogView);
    radiusSeekBar = dialogView.findViewById(R.id.seekBar);
    tvProgress = dialogView.findViewById(R.id.progresText);
    plus = dialogView.findViewById(R.id.plus);
    minus = dialogView.findViewById(R.id.minus);
    radiusSeekBar.setProgress(SrsSettings.getConfiguration().getRadius());
    finalProgress = radiusSeekBar.getProgress();
    tvProgress.setText(getString(R.string.radius_change_text, SrsSettings.getConfiguration().getRadius()));
    radiusSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
            if (b) {
                progress = Math.round(progress / 500) * 500;
                seekBar.setProgress(progress);
                finalProgress = progress;
                radiusSeekBar.setProgress(finalProgress);
                radiusSeekBar.post(new Runnable() {
                    @Override
                    public void run() {
                        radiusSeekBar.setProgress(finalProgress);
                    }
                });
                updateText(finalProgress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (finalProgress < 15000) {
                finalProgress += 500;
                radiusSeekBar.setProgress(finalProgress);
                radiusSeekBar.post(new Runnable() {
                    @Override
                    public void run() {
                        radiusSeekBar.setProgress(finalProgress);
                    }
                });

                updateText(finalProgress);
            }
        }
    });
    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (finalProgress > 500) {
                finalProgress -= 500;
                radiusSeekBar.setProgress(finalProgress);
                updateText(finalProgress);
            }
        }
    });
    builder.setPositiveButton("Ok", (dialog, i) -> {
        if (listener != null) {
            listener.onRadiusChanged(radius);
        }
        dismiss();
    });
    builder.setNegativeButton("Cancel", null);
    return builder.create();


}

private void updateText(int progress) {
    tvProgress.setText(getString(R.string.radius_change_text, progress));
    radius = progress;
}

}

我根本不想隐藏标题或任何内容,所有功能都可以在Android> 5上运行,但是在该版本上,它崩溃了,这是一个例外:

05-13 14:28:56.953 9221-9221/com.---.---.---E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.---.---.---, PID: 9221
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:373)
    at com.android.internal.app.AlertController.installContent(AlertController.java:244)
    at android.app.AlertDialog.onCreate(AlertDialog.java:356)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:465)
    at android.app.Dialog.show(Dialog.java:288)
    at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:416)
    at android.support.v4.app.Fragment.performStart(Fragment.java:2380)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6145)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

1 个答案:

答案 0 :(得分:0)

我找到了问题,覆盖onCreateView和onCreateDialog都导致了该问题。我只是删除了onCreateView,一切都正常了。