Android Q-Android 5-5.1(API 21-22)上的WebView崩溃Resources $ NotFoundException:字符串资源ID#0x2040002

JPM*_*JPM 12 android android-webview

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

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

问题是活动和片段根本不会膨胀,因为缺少String资源。这是一些堆栈跟踪(它似乎找不到String资源):

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

小智 23

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

之后,它在 Android 5 上构建和渲染没有问题。


eri*_*icn 18

这似乎是 appcompat 1.1.0 的错误 - https://issuetracker.google.com/issues/141132133

虽然您可以尝试降级 appcompat 或等待修复,但您也可以尝试

使用自定义 WebView:

private fun Context.getLollipopFixWebView(): Context {
    return if (Build.VERSION.SDK_INT in 21..22) {
        createConfigurationContext(Configuration())
    } else this
}

/**
 * Workaround appcompat-1.1.0 bug https://issuetracker.google.com/issues/141132133
 */
class LollipopFixWebView(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :
    WebView(context.getLollipopFixWebView(), attrs, defStyle)
Run Code Online (Sandbox Code Playgroud)

或者将此解决方法添加到 WebView 的父 Activity:

    // Workaround appcompat-1.1.0 bug https://issuetracker.google.com/issues/141132133
    override fun applyOverrideConfiguration(overrideConfiguration: Configuration) {
        if (Build.VERSION.SDK_INT in 21..22) {
            return
        }
        super.applyOverrideConfiguration(overrideConfiguration)
    }
Run Code Online (Sandbox Code Playgroud)

积分和荣誉https://github.com/ankidroid/Anki-Android/issues/5507 那里的人认为它也发生在 Android 7 上,但我无法复制

更新

自定义 WebView 解决方案可能会引入一个新问题:所有 Android 版本都不显示键盘。

所以我们需要设置isFocusableisFocusableInTouchMode自定义 WebView 类来防止此类问题

class LollipopFixWebView : WebView {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context.getLollipopFixWebView(), attrs, defStyleAttr) {
        isFocusable = true
        isFocusableInTouchMode = true
    }
}
Run Code Online (Sandbox Code Playgroud)

免责声明

我的minSdk版本是 21 (Lollipop) 所以不能保证我的解决方案适用于 minSdk 版本较低的应用程序


And*_*ris 12

修复了androidx.appcompat:appcompat:1.2.0-alpha02 只需build.gradle使用以下行更新您的应用程序文件:

implementation "androidx.appcompat:appcompat:1.2.0-alpha02"
Run Code Online (Sandbox Code Playgroud)


Gun*_*ein 8

It seems to be a bug with appcompat 1.1.0 - https://issuetracker.google.com/issues/141132133

Looking at #30 in that discussion, this solved my problem:

// add to gradle module:app
configurations.all {
    resolutionStrategy {
        force 'androidx.appcompat:appcompat:1.1.0-rc01'
    }
}
Run Code Online (Sandbox Code Playgroud)