如果你開(kāi)發(fā)的APP是系統(tǒng)級(jí)的磷雇,即在AndroidManifest.xml的manifest下添加了android:sharedUserId="android.uid.system"漾岳,那么你在使用webview的時(shí)候可能會(huì)出現(xiàn)如下錯(cuò)誤 :Error inflating class android.webkit.WebView
錯(cuò)誤截圖
解決方式智蝠,在調(diào)用webview的activity或者fragment的setContentView()之前,調(diào)用文中末尾的initStatusBar()方法即可正常運(yùn)行拘鞋。
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) {
}
}