Error inflating class android.webkit.WebView happens sporadically in production

前端 未结 5 664
自闭症患者
自闭症患者 2020-12-03 01:00

my application is on Google Play and it runs fine on most of devices (thousands of users). But in rare cases (fraction of percent of daily active users) I get Error in

5条回答
  •  执念已碎
    2020-12-03 01:37

    To fix this problem that appears on Android Lollipop you can use a custom WebView that creates a new configuration only on Android Lollipop (API 21 and 22). Replace the WebView on your XML layout with this custom WebView.

    import android.annotation.TargetApi;
    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;
        }
    }
    

提交回复
热议问题