【问题标题】:How to catch an error from the logcat inside Android App?如何从 Android App 中的 logcat 中捕获错误?
【发布时间】:2019-01-04 14:37:05
【问题描述】:

我在一些没有更新 webview 引擎的手机上遇到下一个错误,或者有一些不被 cordova webview 支持的奇怪浏览器。

07-27 13:50:47.233 14356-14356/com.locknot I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Object.assign is not a function", source: file:///android_asset/www/vendor.6356da77cc5903aae591.bundle.js (1)

我想捕捉这个错误,以便向用户显示一个对话框,说明他的手机不兼容。

我该怎么做?对不起我的英语。

【问题讨论】:

    标签: android cordova error-handling android-logcat


    【解决方案1】:

    您可以扩展 WebChromeClient 类并覆盖其 onConsoleMessage() 方法。此方法从它显示的网站中接收所有控制台消息。最后,通过 setWebChromeClient() 方法将此 WebChromeClient 的实例设置为 WebView。所以 Kotlin 代码看起来像这样:

    webView.webChromeClient = object : WebChromeClient() {
        override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
            if (consoleMessage != null && consoleMessage.message().contains("Uncaught TypeError", false)) {
                // show user a dialog
            }
            return super.onConsoleMessage(consoleMessage)
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多