Androidx 在Android 5.0下WebView崩潰解決

使用androidx后,在布局中引用webview,項(xiàng)目異常崩潰。?

一、自定義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文件?

在當(dāng)前頁面的AndroidManifest文件中,找到使用webview的頁面 加入android:configChanges="uiMode"

四、

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容