Android5.1.1系统打开包含WebView的界面崩溃闪退问题

开发集成了androidx,在Android10真机安装调试未发现任何问题,开发完成后在vivo云测平台上传apk测试后发现在

x7 Android5.1.1系统的真机上有闪退问题,看日志发现是webView问题,部分日志如下:

android.view.InflateException: Binary XML file line #338: Error inflating class android.webkit.WebView
    at android.view.LayoutInflater.createView(LayoutInflater.java:642)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:727)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:786)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:851)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:510)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:420)
    at com.test.www.mvp.ui.fragment.home.GoodsDetailFragment.a(GoodsDetailFragment.java:14)
......
......

可能在Android5.x的其他系统上也有这个问题,网上搜索一波找到解决方法

1.自定义WebView(使用中)

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebView;

import androidx.annotation.RequiresApi;

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

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

将WebView替换为LollipopFixedWebView即可,记得xml文件和代码文件中都替换下

2.将appcompat的库版本降低(未测试)

//implementation 'androidx.appcompat:appcompat:1.1.0'
//修改为
implementation 'androidx.appcompat:appcompat:1.0.2'

over!


版权声明:本文为i996573526原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。