appcompat:1.1.0 사용중인 경우, Android OS Ver. lollipop 에서 웹뷰 오류가 발생한다

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo ...
... Binary XML file ... webview

간단한 해결 법은 두 개정도.

1st. minSdk 상향 조정 : 21 -> 23

2st. lollipop 분기 처리

 - 단, 분기 처리는 하위 버전을 분기하여 관리하는 품이 하나 더 늘어나는 단점이 있다.

implementation 'androidx.appcompat:appcompat:1.1.0'
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.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);
    }

    // To fix Android Lollipop WebView problem create a new configuration on that Android version only
    private static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT == 21 || Build.VERSION.SDK_INT == 22) // Android Lollipop 5.0 & 5.1
            return context.createConfigurationContext(new Configuration());
        return context;
    }
}

+ Recent posts