Why does Android OS 8 WebVew with HTML select tag crash the app

浪子不回头ぞ 提交于 2019-12-03 11:27:19

Looks like this bug: https://issuetracker.google.com/issues/77246450

Don't subclass resources...won't be fixed apparently.

I have same issue on Android 8.0 . finally i solve it. Try to update your compileSdkVersion to 26, and update your com.android.support:appcompat-v7 to 26.

If anyone is still having this issue, I found that it wasn't even my code that was subclassing the Resources class but rather the Google support library version that I was using. Updated the support library version and it worked like a charm!

I digged into crash logs. Though this answer does not solve your problem, you might get some useful insights

I am not sure why it's happening on Android 8.0,i was not able to reproduce this on Android 8.0 emulator though

In AndroidX land (limited to a minimum compileSDKVersion 28), I was also getting this issue on a Marshmallow emulator with a popup spinner. I don't go anywhere near Resources so this is still a platform issue with one of the support libs.

I managed to get it working - not by moving the webview to an Activity (although I did try that, it was unnecessary), - but by adding it programmatically with a wrapped context and a standard AppCompat theme:

val webView = WebView(ContextThemeWrapper(activity, R.style.Theme_AppCompat_Light))
binding.webViewContainer.addView(webView)

I don't understand wtf is going on here but right now it works. Good luck people!

EDIT I have looked into this a lot more and found the dependency which is causing issues for me. A particular version of the material components library is actually inducing this in webviews (1.1.0-alpha06 to be precise). I have asked a question on it here, with a sample project.

After some investigation, I've isolated the issue to WebView inside Fragment on OS8 only. My workaround is to use Activity instead of Fragment for that particular flow. It seems to me an Android defect in Fragment.

Maybe you use custom ContextWrapper in your Activity class. In my case, I override attachBaseContext method. Check this method and use super.attachBaseContext(newBase).

Actually found a workaround for this a little while ago that allowed us to continue subclassing Resources and not crash our WebViews. The caveat is we can't let our WebView see or interact with our resources subclass AND we must create the WebView programmaticaly. The first step is exposing method in the Resources subclass to pull the original resources back out.

Lets say our resources subclass is called CustomResourcesWrapper and our ContextWrapper subclass is called CustomContextWrapper.

First we update the CustomResourcesWrapper so we can access the original Resources object

public class CustomResourcesWrapper {

    public static Resources findOriginalResources(Context context) {
        if (context.getResources() instanceof CustomResourcesWrapper) {
            return ((CustomResourcesWrapper) context.getResources()).getOriginalResources();
        }
        if (context instanceof ContextWrapper) {
            return findOriginalResources(((ContextWrapper) context).getBaseContext());
        }
        return context.getResources();
    }

    private Resources getOriginalResources() {
        return originalResources;
    }
}

We're also assuming the CustomContextWrapper looks something like this...

public class CustomContextWrapper extends ContextWrapper {

    private final Resources wrappedResources;

    public CustomContextWrapper(Context base, Resources resources) {
        super(base);
        this.wrappedResources = resources;
    }

    @Override
    public Resources getResources() {
        return wrappedResources;
    }
}

Then we create a static helper method to "unwrap" our custom resources and hide them

// the method name is a bit of a misnomer, 
// we're actually unwrapping, then re-wrapping
public static Context unwrapCustomContext(Context wrapped) {
    Resources originalResources = CustomResourcesWrapper.findOriginalResources(wrapped);
    Context customUnwrappedContext = new CustomContextWrapper(wrapped, originalResources);
    return new ContextThemeWrapper(customUnwrappedContext, android.support.v7.appcompat.R.style.Theme_AppCompat_Light);
}

When it comes time to create a WebView, do it ensure the Context we pass to it runs through the above method i.e. WebView webView = new WebView(unwrapCustomContext(context)). I'm not 100% sure why, but the ContextThemeWrapper is a required part of this hack.

Its look like you are setting Integer value to the Textview. try using String.valueOf(value) to set value in Textview.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!