Android 5-5.1(API 21-22)上的WebView崩溃Resources $ NotFoundException:字符串资源ID#0x2040002 [英] WebView Crash on Android 5-5.1 (API 21-22) Resources$NotFoundException: String resource ID #0x2040002

查看:459
本文介绍了Android 5-5.1(API 21-22)上的WebView崩溃Resources $ NotFoundException:字符串资源ID#0x2040002的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Android应用程序从API 27更新到API 29,并且我发现尝试在基于5.0和/或5.1的模拟器上呈现WebView时崩溃.在运行6.0或更高版本(API 23-29)的模拟器上不会发生此问题.

I am in the process of updating an Android app from API 27 to API 29 and I noticed that I get a crash when trying to render a WebView on an emulator based on 5.0 and/or 5.1. This issue does not happen on an emulator running 6.0 or higher (API 23-29).

我似乎找不到任何有关WebView行为的文档,这些文档会影响5.0或5.1,但是我可以确认使用API​​ 27运行该应用程序时不会发生此问题.我不知所措,因为我不知道是否这是模拟器问题,还是实际的API/设备问题(我认为是后者).

I cannot seem to find any documentation about WebView behaviour that would affect 5.0 or 5.1 but I can confirm the problem doesn't happen when I run the app using API 27. I am at a loss as I don't know if this is an emulator problem or an actual API/device issue (I am thinking the latter).

问题在于,由于缺少String资源,Activity和Fragment根本不会膨胀.这是一些堆栈跟踪信息(它似乎找不到String资源):

The issue is the Activity and Fragment won't inflate at all, due to a missing String resource. Here is some of the stacktrace (it can't seem to find a String resource):

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.MyWebView
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.MyWebView...
Caused by: java.lang.reflect.InvocationTargetException...
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040002

崩溃发生之前,与资源相关的日志中有一些相关的警告:

Before the crash happens, there is some related warnings in the logs related to the resource:

W/chromium: [WARNING:resource_bundle.cc(315)] locale_file_path.empty()
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
W/chromium: [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation
W/chromium: [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup
W/ResourceType: No known package when getting value for resource number 0x02040002

WebView:

public class MyWebView extends WebView {

    public MyWebView (Context context) {
        super(context);
        initialize();
    }

    public MyWebView (Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public MyWebView (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    public MyWebView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initialize();
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(context, attrs, defStyleAttr, privateBrowsing);
        initialize();
    }

    private void initialize() {
        this.clearCache(true);
        this.clearHistory();
        this.getSettings().setJavaScriptEnabled(true);
        this.getSettings().setLoadsImagesAutomatically(true);
        this.getSettings().setUseWideViewPort(true);
        this.getSettings().setAllowContentAccess(false);
        this.getSettings().setAllowFileAccess(false);
        this.getSettings().setAllowFileAccessFromFileURLs(false);
        this.getSettings().setAllowUniversalAccessFromFileURLs(false);
        this.getSettings().setDomStorageEnabled(false);
        this.getSettings().setAppCacheEnabled(false);
        this.getSettings().setDatabaseEnabled(false);
        this.getSettings().setGeolocationEnabled(false);
        this.getSettings().setSaveFormData(false);
        this.getSettings().setSupportMultipleWindows(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            this.getSettings().setSafeBrowsingEnabled(false);
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.example.MyWebView
        android:id="@+id/my_web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

有什么想法我做错了什么,或者API 28或29中有哪些更改可能破坏了这一点?

Any ideas what I am doing wrong or what may have changed in API 28 or 29 that could break this?

推荐答案

我遇到了同样的麻烦,实际上解决问题的方法是将构造函数中的上下文更改为context.getApplicationContext().

I went through the same trouble and actually what solved it was changing the context in the constructor to context.getApplicationContext().

之后,它可以在Android 5上毫无问题地构建和渲染.

After that it's building and rendering with no problems on Android 5.

这篇关于Android 5-5.1(API 21-22)上的WebView崩溃Resources $ NotFoundException:字符串资源ID#0x2040002的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆