如果你开发的APP是系统级的,即在AndroidManifest.xml的manifest下添加了android:sharedUserId="android.uid.system",那么你在使用webview的时候可能会出现如下错误 :Error inflating class android.webkit.WebView
解决方式,在调用webview的activity或者fragment的setContentView()之前,调用文中末尾的initStatusBar()方法即可正常运行。
public static void hookWebView() {
int sdkInt = Build.VERSION.SDK_INT;
try {
Class factoryClass = Class.forName("android.webkit.WebViewFactory");
Field field = factoryClass.getDeclaredField("sProviderInstance");
field.setAccessible(true);
Object sProviderInstance = field.get(null);
if (sProviderInstance != null) {
return;
}
Method getProviderClassMethod;
if (sdkInt > 22) {
getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass");
} else if (sdkInt == 22) {
getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass");
} else {
return;
}
getProviderClassMethod.setAccessible(true);
Class providerClass = (Class) getProviderClassMethod.invoke(factoryClass);
Class delegateClass = Class.forName("android.webkit.WebViewDelegate");
Constructor providerConstructor = providerClass.getConstructor(delegateClass);
if (providerConstructor != null) {
providerConstructor.setAccessible(true);
Constructor declaredConstructor = delegateClass.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
sProviderInstance = providerConstructor.newInstance(declaredConstructor.newInstance());
field.set("sProviderInstance", sProviderInstance);
}
} catch (Throwable e) {
}
}