wendux / DSBridge-Android

:earth_americas: A modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native.
3.78k stars 618 forks source link

弹窗出现BadTokenException #88

Open jaysowen opened 5 years ago

jaysowen commented 5 years ago

使用的依赖是:implementation 'com.github.wendux:DSBridge-Android:master-SNAPSHOT'

Fatal Exception: android.view.WindowManager$BadTokenException Unable to add window -- token android.os.BinderProxy@e1d5cae is not valid; is your activity running? keyboard_arrow_up android.view.ViewRootImpl.setView (ViewRootImpl.java:798) android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:359) android.view.WindowManagerImpl.addView (WindowManagerImpl.java:93) android.app.Dialog.show (Dialog.java:357) arrow_right wendu.dsbridge.DWebView$6.onJsAlert (DWebView.java:704) xf.a (SourceFile:424) afc.run (Unknown Source:3)

wendux commented 5 years ago

compile 'com.github.wendux:DSBridge-Android:3.0-SNAPSHOT' 试试?

wufei608 commented 5 years ago

compile 'com.github.wendux:DSBridge-Android:3.0-SNAPSHOT' 试试?

same issue

wendux commented 5 years ago

这个应该和应用主题相关,导致alert对话框实现不兼容,有两个方法可以参考:

  1. 可以参考demo对比一下, 是否主题问题
  2. 自己实现onJsAlert
liyanxi commented 5 years ago

same issue

liyanxi commented 5 years ago

The reason for this exception is that, as the exception message says, the activity has finished but you are trying to display a dialog with a context of the finished activity. Since there is no window for the dialog to display the android runtime throws this exception.

yabee5566 commented 3 years ago

I override onJsAlert(...) in the WebChromeClient passed in and do extra check whether that the activity is finishing or not.

override fun onJsAlert(view: WebView?, url: String?, message: String?, result: JsResult?): Boolean {
                // To avoid WindowManager$BadTokenException
                if (this@MyWebActivity.isFinishing) {
                    return true
                }
                return super.onJsAlert(view, url, message, result)
            }