【问题标题】:Exception on new WebView on Lollipop 5.1 devices [duplicate]Lollipop 5.1 设备上的新 WebView 异常[重复]
【发布时间】:2018-03-30 07:46:50
【问题描述】:

我尝试将 WebView 添加到我的活动中:

WebView webView = new WebView(this);

在所有设备上都可以正常工作,但在 Android Lollipop 5.1(仅限)设备上,我在这一行遇到了异常:

10-18 12:04:29.741 13131-13131/xx.xxxxxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: xx.xxxxxx, PID: 13131
java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xxxxxx/xx.xxxxxx.activity.login.UseActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    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.access$100(WebViewChromium.java:96)
        at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:263)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:123)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.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 android.webkit.WebView.<init>(WebView.java:449)
        at xx.xxxxxx.activity.login.UseActivity.onActivityCreate(UseActivity.java:69)
        at xx.xxxxxx.activity.BaseActivity.onCreate(BaseActivity.java:148)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        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) 

我做错了什么以及在 Android Lollipop 5.1 设备上解决问题的最佳方法是什么。

【问题讨论】:

  • Resources$NotFoundException
  • 是的,但为什么只在 5.1 设备上例外(可能是 5.1,未安装 Google Play 服务或未在设备上注册 Google 帐户)?
  • 分享您的 UseActivity 和 BaseActivity 代码
  • 检查 google ps 是否存在
  • @IntelliJ Amiya,目前不可能,这是针对没有统计信息的制作用户,可能将来我会尝试添加检查谷歌播放服务状态

标签: java android webview


【解决方案1】:

这可以通过捕获错误来解决,然后使用应用程序上下文创建 WebView:

try {
    webView = new WebView(this);
}
catch (Resources.NotFoundException e) {
    // Some older devices can crash when instantiating a WebView, due to a Resources$NotFoundException
    // Creating with the application Context fixes this, but is not generally recommended for view creation
    webView = new WebView(getApplicationContext());
}

通常不鼓励使用应用程序上下文创建视图,但在这里是一个不错的后备方案。

【讨论】:

  • 您找到解决方案了吗?我有一个很大的分支,并且在 5.1 上的那个分支上 webview 不起作用。而我的主分支工作得很好。我在 manifest/build gradle 中没有任何变化
  • @ronocod 你有这方面的参考吗?
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 2020-08-03
  • 1970-01-01
  • 2019-05-09
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多