最近线上后台发现一个崩溃问题,在Android5.x上,创建webview时会发生carsh,报错信息:

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003

at android.content.res.Resources.getText(Resources.java:318)

at android.content.res.VivoResources.getText(VivoResources.java:123)

at android.content.res.Resources.getString(Resources.java:404)

at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:694)

at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:618)

at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631)

at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780)

at com.android.org.chromium.android_webview.AwContents.(AwContents.java:619)

at com.android.org.chromium.android_webview.AwContents.(AwContents.java:556)

at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:312)

at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:96)

at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:264)

在大部分的vivo 5.x系统下会出现问题,非5.x系统不会出现。

解决办法一

在Android5.x上通过解决自定义WebView

网上大部分都是这种方式解决的。

public class LollipopFixedWebView extends WebView {

public LollipopFixedWebView(Context context) {

super(getFixedContext(context));

}

public LollipopFixedWebView(Context context, AttributeSet attrs) {

super(getFixedContext(context), attrs);

}

public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {

super(getFixedContext(context), attrs, defStyleAttr);

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);

}

public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {

super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);

}

public static Context getFixedContext(Context context) {

if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) // Android Lollipop 5.0 & 5.1

return context.createConfigurationContext(new Configuration());

return context;

}

}

主要核心方法是getFixedContext(context),根据版本号配置不同的context.

解决方法二

如果项目中使用"androidx.appcompat:appcompat:1.1.0" ,替换为"androidx.appcompat:appcompat:1.0.2"

1.1.0版本webview在Android5.x上有问题,恢复到1.0.2之后确实解决了此问题,目前没有具体追踪,后续会跟上。

"androidx.appcompat:appcompat:1.1.0"发布时间:2019/09/05,到现在更新一个月时间~

"androidx.appcompat:appcompat:1.0.2"发布时间:2018/10/07,到现在更新一年时间~

为了保证项目稳定,build gradle依赖更新时间要适当。

最后,希望此篇博客对大家有所帮助,欢迎提出问题及建议共同探讨,如有兴趣可以关注我的博客,谢谢!

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐