android webview 报错,Android WebView 5.x 系统下 Resources$NotFoundException异常处理
最近線上后臺發現一個崩潰問題,在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依賴更新時間要適當。
最后,希望此篇博客對大家有所幫助,歡迎提出問題及建議共同探討,如有興趣可以關注我的博客,謝謝!
總結
以上是生活随笔為你收集整理的android webview 报错,Android WebView 5.x 系统下 Resources$NotFoundException异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ajax 传字符串到后台,JSON.st
- 下一篇: c if语句多个条件判断顺序_Java中