Exception handling of Resources$NotFoundException in Android WebView 5.x system

Keywords: Mobile Android Java Google Gradle

Recently, a crash problem was found in the online background. On Android 5. X, when creating a webview, a carsh will occur, with an error message:

 

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.<init>(AwContents.java:619)
    at com.android.org.chromium.android_webview.AwContents.<init>(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)

In most vivo 5.x systems, there will be problems, and non-5.x systems will not.

Solution 1

Solve the problem of customizing WebView on Android 5. X

Most of the Internet is solved in this way.

 

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;
    }
}

The main core method is getFixedContext(context). Different contexts are configured according to the version number

Solution 2

If "Android. Appcompat: appcompat: 1.1.0" is used in the project, replace with "Android. Appcompat: appcompat: 1.0.2"

webview in version 1.1.0 has problems with Android 5. X. after recovering to 1.0.2, this problem has been solved. At present, there is no specific tracking. We will keep up with it later.

Official documents: https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn

"Android x.appcompat: appcompat: 1.1.0" released on September 5, 2019, updated one month now~

"Android x.appcompat: appcompat: 1.0.2" released on October 7, 2018, updated one year now~

In order to ensure the stability of the project, build gradle depends on the update time to be appropriate.

Posted by Gambler on Wed, 11 Dec 2019 06:22:05 -0800