#xff0c;当Web出现异常时
发布时间:2025-06-24 20:55:15 作者:北方职教升学中心 阅读量:982
2、
1、需要设置addJavascriptobject()和#xff0c;即reloadFun函数。要解决webview内容不可见的问题,#xff0c;当Web出现异常时,
背景:
App主页使用大量Webview容器(超过10个)显示图表信息,最新发现的bugly上有一些关于浏览器Nativee的信息 Crash,以下:
经调查,WebView渲染失败导致Crash,通过webviewew可以使用.loadUrl("chrome://crash")模拟。在上述代码中c;新的Web容器id与删除相同,因此,
注意事项:
1、
解决方案:。但这样做的应用程序c;但是主页上的webview内容却看不到,看到白色/黑色背景,体验极差。还需要,删除原来的Web容器重新创建Web容器,代码如下:class ReportWebView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : wendu.dsbridge.BaseWebView(context, attrs) { var reloadFun: ((any: ReportWebView) -> Unit)? = null private var parentViewGroup: ViewGroup? = null init { webViewClient = CustomWebViewClient() } override fun onAttachedToWindow() { super.onAttachedToWindow() parentViewGroup = parent as? ViewGroup } private inner class CustomWebViewClient : WebViewClient() { override fun onRenderProcessGone(view: WebView?, detail: RenderProcessGoneDetail?): Boolean { // 所有web都是crash,所有这些都需要重建 recreateWebViewAndReload(view) return true } } private fun recreateWebViewAndReload(view: WebView?) { val originalUrl = view?.url// 原webview地址 val isVisible = view?.isVisible val lp = this.layoutParams // 移除旧的 WebView val index = indexInParent() if (parentViewGroup != null) { parentViewGroup?.removeView(this) } destroy()// 销毁 // 重新创建 WebView val newWebView = ReportWebView(context) reloadFun?.invoke(newWebView) newWebView.reloadFun = reloadFun newWebView.id = id newWebView.layoutParams = lp newWebView.isVisible = isVisible.nullOr(false) originalUrl?.let { newWebView.loadUrl(it) } // 将新的 WebView 添加回布局 parentViewGroup?.addView(newWebView, index) // 更新引用 parentViewGroup = newWebView.parent as? ViewGroup } private fun indexInParent(): Int { return parentViewGroup?.indexOfChild(this) ?: -1 }}。虽然没有Crash,2、
class ReportWebView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : wendu.dsbridge.BaseWebView(context, attrs) { var reloadFun: ((any: ReportWebView) -> Unit)? = null private var parentViewGroup: ViewGroup? = null init { webViewClient = CustomWebViewClient() } override fun onAttachedToWindow() { super.onAttachedToWindow() parentViewGroup = parent as? ViewGroup } private inner class CustomWebViewClient : WebViewClient() { override fun onRenderProcessGone(view: WebView?, detail: RenderProcessGoneDetail?): Boolean { // 所有web都是crash,所有这些都需要重建 recreateWebViewAndReload(view) return true } } private fun recreateWebViewAndReload(view: WebView?) { val originalUrl = view?.url// 原webview地址 val isVisible = view?.isVisible val lp = this.layoutParams // 移除旧的 WebView val index = indexInParent() if (parentViewGroup != null) { parentViewGroup?.removeView(this) } destroy()// 销毁 // 重新创建 WebView val newWebView = ReportWebView(context) reloadFun?.invoke(newWebView) newWebView.reloadFun = reloadFun newWebView.id = id newWebView.layoutParams = lp newWebView.isVisible = isVisible.nullOr(false) originalUrl?.let { newWebView.loadUrl(it) } // 将新的 WebView 添加回布局 parentViewGroup?.addView(newWebView, index) // 更新引用 parentViewGroup = newWebView.parent as? ViewGroup } private fun indexInParent(): Int { return parentViewGroup?.indexOfChild(this) ?: -1 }}。虽然没有Crash,2、
本项目桥接采用DSBridge三方库,要创建Web容器,使用时,拆除了原来的Web容器,最新的Web容器需要使用c;否则会报错。通过设置Webviewclient,重写onrenderProcesgone()返回值强迫返回trueWebview发生异常时表示,自己处理这样,所有Web容器都必须重新创建只能根据Web的可见状态创建可见的Web。也很容易获得新的Web容器,代码如下:
/** * 获得真实的webview,以前的web可能被销毁 * @param id web id */private fun getRealWebView(id: Int): ReportWebView { return mBinding.root.findViewById(id)}。Crash就不会出现在App中。只要同一页面有一个渲染异常,所有Web容器都会出现异常,因此,