Java android.view.InflateException 错误膨胀类 android.webkit.WebView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41025200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.view.InflateException Error inflating class android.webkit.WebView
提问by Valerio
In Lollipop (API 22) every time in my application I show a webview the application crashes. I have multiple crashes in my android developer console related to this event.
在 Lollipop (API 22) 中,每次在我的应用程序中显示 webview 时,应用程序都会崩溃。我的 android 开发者控制台中有多次与此事件相关的崩溃。
No need to say that it works on Android 4, 6 and 7.
无需说它适用于 Android 4、6 和 7。
Reading the stack trace (posted at the end of this post), something bugs me
阅读堆栈跟踪(发布在本文末尾),有些东西让我感到困惑
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
I searched in the generated R.java without any luck, obviously because the ID does not exists, but it was worth a try.
我在生成的 R.java 中搜索没有任何运气,显然是因为 ID 不存在,但值得一试。
Googling around the problem seems to be related to how lollipop handles the webview. I started a fresh AVD with lollipop based on a device I found on the crash reporter in GDC, and I can reproduce the problem.
谷歌搜索这个问题似乎与棒棒糖如何处理 web 视图有关。我根据我在 GDC 的崩溃报告器上找到的设备,用棒棒糖开始了一个新的 AVD,我可以重现这个问题。
Full stack trace:
完整的堆栈跟踪:
android.view.InflateException: Binary XML file line #7: Error inflating class android.webkit.WebView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)?
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)?
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)?
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)?
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)?
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)?
at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67)?
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)?
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)?
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)?
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)?
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)?
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:541)?
at android.os.Handler.handleCallback(Handler.java:739)?
at android.os.Handler.dispatchMessage(Handler.java:95)?
at android.os.Looper.loop(Looper.java:135)?
at android.app.ActivityThread.main(ActivityThread.java:5254)?
at java.lang.reflect.Method.invoke(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:372)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)?
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
at android.content.res.Resources.getText(Resources.java:299)
at android.content.res.Resources.getString(Resources.java:385)
at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:684)
at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631)
at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780)
at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:619)
at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:556)
at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:311)
at com.android.webview.chromium.WebViewChromium.access0(WebViewChromium.java:96)
at com.android.webview.chromium.WebViewChromium.run(WebViewChromium.java:263)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:123)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.run(WebViewChromium.java:110)
at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:107)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:260)
at android.webkit.WebView.<init>(WebView.java:554)
at android.webkit.WebView.<init>(WebView.java:489)
at android.webkit.WebView.<init>(WebView.java:472)
at android.webkit.WebView.<init>(WebView.java:459)
at java.lang.reflect.Constructor.newInstance(Native Method)?
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)?
at android.view.LayoutInflater.createView(LayoutInflater.java:607)?
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)?
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)?
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)?
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)?
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)?
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)?
at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67)?
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)?
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)?
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)?
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)?
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)?
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:541)?
at android.os.Handler.handleCallback(Handler.java:739)?
at android.os.Handler.dispatchMessage(Handler.java:95)?
at android.os.Looper.loop(Looper.java:135)?
at android.app.ActivityThread.main(ActivityThread.java:5254)?
at java.lang.reflect.Method.invoke(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:372)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)?
采纳答案by u10683256
if you use "androidx.appcompat:appcompat:1.1.0" , try "androidx.appcompat:appcompat:1.0.2" instead. it seems that 1.1.0 dont handle bug with webview in android 5.1.1.
如果您使用 "androidx.appcompat:appcompat:1.1.0" ,请尝试使用 "androidx.appcompat:appcompat:1.0.2"。似乎 1.1.0 不处理 android 5.1.1 中的 webview 错误。
Feb-2020 update: Reverting to 1.0.2
stopped working for many people (including my app), but using the current version of androidx.appcompat:appcompat:1.2.0-alpha02
did fix the crash. (I was seeing it on a Huawei P8 Lite running Android 5.0 during Google's automated "Pre-launch report" testing).
2020 年 2 月更新:恢复到1.0.2
对许多人(包括我的应用程序)停止工作,但使用当前版本androidx.appcompat:appcompat:1.2.0-alpha02
确实修复了崩溃。(在 Google 的自动“预发布报告”测试期间,我在运行 Android 5.0 的华为 P8 Lite 上看到了它)。
回答by ikostet
Try use for creating webview:
尝试用于创建 webview:
mWebView = new WebView(getActivity().createConfigurationContext(new Configuration()));
回答by SpaceBison
Warning: this workaround might also break some things; see the comments for details
警告:此解决方法也可能会破坏某些内容;详情见评论
If you'd like to inflate the WebView from an XML layout, you can wrap it in a nice little subclass (based on ikostet's answer):
如果您想从 XML 布局扩展 WebView,您可以将它包装在一个漂亮的小子类中(基于ikostet 的回答):
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);
}
public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);
}
public static Context getFixedContext(Context context) {
return context.createConfigurationContext(new Configuration());
}
}
EDIT: now even nicer with Kotlin
编辑:现在使用 Kotlin 更好
class LollipopFixedWebView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : WebView(context.createConfigurationContext(Configuration()), attrs, defStyleAttr, defStyleRes)
回答by snachmsm
my advice is to use custom/new Configuration
only when "original one" is causing problems, so on Lollipop only. @SpaceBizon code work well till Android 8.x, on 9 and Q (currenly beta) every select/dropdown press won't show AlertDialog
picker, instead of that memory leak occurs... below fixed getFixedContext
method with "iffed" proper version code
我的建议是Configuration
仅当“原始版本”引起问题时才使用自定义/新版本,因此仅在 Lollipop 上使用。@SpaceBizon 代码在 Android 8.x 之前运行良好,在 9 和 Q(当前测试版)上,每个选择/下拉按钮都不会显示AlertDialog
选择器,而不是发生内存泄漏......在getFixedContext
带有“iffed”正确版本代码的固定方法下方
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;
}
}
回答by S. Gissel
戴文锦 was right for me too. But downgrading just androidx.appcompat:appcompat:1.1.0 to 1.0.2 did not succeed.
戴文锦也适合我。但是仅将 androidx.appcompat:appcompat:1.1.0 降级到 1.0.2 没有成功。
I downgraded all my previously upgraded androidx versions from
我将之前升级的所有 androidx 版本从
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha10'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta04'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta04'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.core:core:1.2.0-alpha04'
back to
回到
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha09'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta03'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-rc01'
implementation 'androidx.preference:preference:1.1.0-rc01'
implementation 'androidx.core:core:1.2.0-alpha03'
A reminder that Googles code is by far not bug free and well tested and that someone should never upgrade the versions flippantly.
提醒 Google 的代码到目前为止并非没有错误且经过良好测试,并且任何人都不应该轻率地升级版本。
回答by A R Mythili Saran
If you are using androidx.appcompat:appcompat:1.1.0, either change to androidx.appcompat:appcompat:1.0.2 or if you want to use DayNight Theme, override applyOverrideConfiguration in your activity as follows. (Note: This requires app restart while switching from Dark Theme to Light Theme and vice versa).
如果您使用的是 androidx.appcompat:appcompat:1.1.0,请更改为 androidx.appcompat:appcompat:1.0.2 或者如果您想使用 DayNight 主题,请按如下方式覆盖您的活动中的 applyOverrideConfiguration。(注意:这需要在从深色主题切换到浅色主题时重新启动应用程序,反之亦然)。
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
if (Build.VERSION.SDK_INT in 21..25 && (resources.configuration.uiMode == applicationContext.resources.configuration.uiMode)) {
return
}
super.applyOverrideConfiguration(overrideConfiguration)
}
回答by display name
/**
* Customized WebView to avoid crashing on Android 5 and 6 (API level 21 to 23)
* If the Android System WebView is old and is not updated, the WebView view inflation fails.
* To reproduce the issue, try on OS 5 or 6 with Android System WebView 72.0.3626.76 (this version is just a reference point from being which we saw no crashes)
*/
class BrilliantWebView : WebView {
companion object {
private fun getBrilliantContext(context: Context?) =
if (!OSUtils.hasNougat()) // OS < 24 or OS < 7.0
context?.createConfigurationContext(Configuration())
else
context
}
constructor(context: Context?) : super(getBrilliantContext(context))
constructor(context: Context?, attrs: AttributeSet?) : super(getBrilliantContext(context), attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(getBrilliantContext(context), attrs, defStyleAttr)
}
回答by Manuel
I was able to reproduce the crash on API 21 in the emulator.
我能够在模拟器中重现 API 21 上的崩溃。
So I tried to add this to the implementation, as described in the docs:
所以我尝试将其添加到实现中,如文档中所述:
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"
}
Adding appcompat-resources
did notfix the issue.
添加appcompat-resources
并没有解决问题。
Maybe a future version will, but I wanted to mention that there seems to be a complementary resource library that is supposed to address this issue. So when trying to fix this with a new version of appcompat
, add the appcompat-resources
library with the same version.
也许未来的版本会,但我想提一下,似乎有一个补充资源库可以解决这个问题。因此,当尝试使用 的新版本修复此问题时appcompat
,请添加appcompat-resources
具有相同版本的库。
Reverting to androidx.appcompat:appcompat:1.0.2
did fix the issue as a workaround.
恢复为androidx.appcompat:appcompat:1.0.2
确实解决了该问题作为解决方法。
回答by S. Gissel
If you are not relying on DayNight theme switching (or other UiMode events), you can add android:configChanges="uiMode"to the webview activity manifest to prevent AppCompatDelegate updating the Resources configuration and thereby messing up the webview inflation.
如果您不依赖 DayNight 主题切换(或其他 UiMode 事件),您可以将android:configChanges="uiMode" 添加到 webview 活动清单,以防止 AppCompatDelegate 更新资源配置,从而弄乱 webview 膨胀。
回答by Ash
The issue was solve for my by downgrading to implementation 'androidx.appcompat:appcompat:1.0.2'
通过降级到实现“androidx.appcompat:appcompat:1.0.2”解决了我的问题