AlertDialog报错AndroidRuntimeException: requestFeature() must be called before adding content

Android6.0及以下版本使用AlertDialog会报该错误,原因是在dialog.show()之后操作了Window,例如给dialog设置进出动画。
举例

  dialog.setView(viewDialog);
        dialog.show();
//使用alertDialog在6.0以下版本操作widow必须放在dialog.show()之后
        Window window;
indow=getDialogWindow(mContext,dialog);
        window=dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.MyDialogScale);//设置动画效果
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setGravity(mGravity);
        window.setAttributes(lp);

你可能感兴趣的:(AlertDialog报错AndroidRuntimeException: requestFeature() must be called before adding content)