关闭并再次单击后如何修复对话框中的错误?

How to fix error in Dialog after dismiss and click again?

我正在开发 webview 应用程序,OnJsAlert 中的问题是当我单击 Dialog 它在 关闭 之后打开并再次单击它 停止 我的应用程序,抱歉我无法从 调试.

这是我的MainActivity.class

@Override
public boolean onJsAlert(WebView view, String url, final String alertSource, final JsResult alertResult) {

    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    alertDialog.setContentView(R.layout.activity_alert);

    alertDialog.setCancelable(true);

    TextView alertMessage = alertDialog.findViewById(R.id.alert_text);

    alertMessage.setText(alertSource);

    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {

            alertResult.cancel();

        }


    });

    alertDialog.show();

    return true;

}

已编辑:日志

W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed. E/ViewRootImpl: sendUserActionEvent() mView == null W/System.err: android.util.AndroidRuntimeException: requestFeature() must be called before adding content W/System.err: at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:331) at android.app.Dialog.requestWindowFeature(Dialog.java:1057) at com.xcoder.stepview.MainActivity.onJsAlert(MainActivity.java:285) at com.android.webview.chromium.WebViewContentsClientAdapter.handleJsAlert(WebViewContentsClientAdapter.java:606) at com.android.org.chromium.android_webview.AwContentsClientBridge.handleJsAlert(AwContentsClientBridge.java:73) at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5641) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104) at dalvik.system.NativeStart.main(Native Method) A/libc: Fatal signal 6 (SIGABRT) at 0x00002c6d (code=-6), thread 11373 (xcoder.stepview) Application terminated.

你的 alertDialog 是在调用 public boolean onJsAlert(...) 方法之前创建的,当它第二次被调用时你得到 AndroidRuntimeException: requestFeature() must be called before adding content 因为你不能在创建的对话框中使用 requestWindowFeature()。您必须在此方法中创建新的对话框实例或重新使用全局定义的对话框。