Androidx 在Android 5.0下WebView崩溃解决

使用androidx后,在布局中引用webview,项目异常崩溃。 

一、自定义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);

    }

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

    }

}

二、回退版本

dependencies {

    def appcompat_version = "1.1.0"

    implementation "androidx.appcompat:appcompat:$appcompat_version"

    // For loading and tinting drawables on older versions of the platform

    implementation "androidx.appcompat:appcompat-resources:$appcompat_version"

}

三、配置AndroidManifest文件 

在当前页面的AndroidManifest文件中,找到使用webview的页面 加入android:configChanges="uiMode"

四、

new WebView(getActivity().createConfigurationContext(new Configuration()));

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。