You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 11选中WebView文本时App崩溃如何解决

Android 11设备选中文本触发WebView崩溃解决方案

问题现象

在搭载Android 11系统的设备上运行App时,用户选中WebView组件内展示的文本会直接触发崩溃,抛出异常android.content.res.Resources$NotFoundException,完整崩溃堆栈如下:

Fatal Exception: android.content.res.Resources$NotFoundException: Resource ID #0x20c000e
       at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:285)
       at android.content.res.Resources.getInteger(Resources.java:1297)
       at org.chromium.ui.base.DeviceFormFactor.b(chromium-TrichromeWebViewGoogle.aab-stable-428010133:4)
       at org.chromium.content.browser.selection.SelectionPopupControllerImpl.n(chromium-TrichromeWebViewGoogle.aab-stable-428010133:1)
       at WJ.onCreateActionMode(chromium-TrichromeWebViewGoogle.aab-stable-428010133:8)
       at ka0.onCreateActionMode(chromium-TrichromeWebViewGoogle.aab-stable-428010133:2)
       at com.android.internal.policy.DecorView$ActionModeCallback2Wrapper.onCreateActionMode(DecorView.java:3842)
       at com.android.internal.policy.DecorView.startActionMode(DecorView.java:1399)
       at com.android.internal.policy.DecorView.startActionModeForChild(DecorView.java:1354)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:1048)
       at android.view.View.startActionMode(View.java:8359)
       at org.chromium.content.browser.selection.SelectionPopupControllerImpl.K(chromium-TrichromeWebViewGoogle.aab-stable-428010133:10)
       at Ga0.a(chromium-TrichromeWebViewGoogle.aab-stable-428010133:10)
       at Na0.i(chromium-TrichromeWebViewGoogle.aab-stable-428010133:4)
       at rS.run(chromium-TrichromeWebViewGoogle.aab-stable-428010133:3)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:246)
       at android.app.ActivityThread.main(ActivityThread.java:8506)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

问题根因

该问题是特定版本Google WebView(版本号428010xxx系列,即堆栈中标识的TrichromeWebViewGoogle版本)和App侧资源配置逻辑冲突导致:

  1. 触发时机:用户选中文本时,WebView会弹出文本选择操作菜单,创建ActionMode前Chromium内核会调用DeviceFormFactor类读取系统隐藏整数资源0x20c000e(对应com.android.internal.R.integer.config_webviewDeviceFormFactor),判断当前设备是手机/平板形态,用于适配选择菜单的布局。
  2. 崩溃触发条件:App侧修改了资源读取链路,导致内核无法通过当前Context的Resources对象读到该系统资源,常见触发场景包括:
    • 重写了Application或Activity的getResources()方法,返回了自定义包装的Resources实例,做字体适配、多语言适配时没有正确代理getInteger()等资源读取方法,遗漏了系统隐藏资源的查询逻辑
    • WebView初始化时使用了配置错误的Context,比如用Application Context初始化、传入了主题配置错误的ContextThemeWrapper,破坏了系统资源的读取链路
    • 资源混淆/优化规则错误,误删了系统资源ID的映射(该场景占比极低)

解决方案

按照优先级从高到低选择修复方案:

  • 修复自定义Resources逻辑
    排查项目中是否存在重写getResources()的逻辑,尤其是做字体大小适配、多语言切换的自定义Resources包装类,确保所有资源读取方法都正确代理给系统原生Resources对象。如果自定义逻辑不需要特殊处理整数类型资源,直接调用父类的getInteger()方法即可,不要拦截或抛出异常。
  • 校验WebView使用的Context
    禁止使用Application Context初始化WebView,尽量使用当前Activity的原始Context创建WebView实例。如果需要给WebView单独配置主题,确保使用的ContextThemeWrapper继承自系统原生主题,没有修改资源读取的底层实现。
  • 兜底兼容方案
    如果短时间内无法定位修改自定义Resources逻辑,可以在WebView所在的Activity中重写ActionMode创建相关方法,捕获该异常避免崩溃,示例代码:
    @Override
    public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
        try {
            return super.onWindowStartingActionMode(callback, type);
        } catch (Resources.NotFoundException e) {
            return null;
        }
    }
    
    @Override
    public ActionMode startActionMode(ActionMode.Callback callback, int type) {
        try {
            return super.startActionMode(callback, type);
        } catch (Resources.NotFoundException e) {
            return null;
        }
    }
    

    注意:该方案仅为兜底避免崩溃,优先推荐修复资源读取链路,避免其他WebView场景触发同类资源异常。

  • 补充说明
    该问题仅出现在Android 11搭配特定版本WebView的场景,后续WebView版本升级后会修复部分机型的问题,但不能依赖用户升级系统WebView,必须在App侧做兼容处理。

内容的提问来源于stack exchange,提问作者Laxmi Kant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 20:24:31