14

I try add WebView to my activity:

WebView webView = new WebView(this);

On all devices this work fine, but on Android Lollipop 5.1 (only) devices I got exception in this line:

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) 

What I'm doing wrong and what is best way for solution on Android Lollipop 5.1 devices.

Rafael T
  • 15,401
  • 15
  • 83
  • 144
Tapa Save
  • 4,769
  • 5
  • 32
  • 54
  • `Resources$NotFoundException` – IntelliJ Amiya Oct 18 '17 at 12:24
  • yes, but why exceptions only on 5.1 devices (probably 5.1 with not installed Google Play Services or not-registered Google account on deveice)? – Tapa Save Oct 18 '17 at 12:29
  • share your UseActivity & BaseActivity code – Mehta Oct 18 '17 at 12:29
  • check google ps is present or not – IntelliJ Amiya Oct 18 '17 at 12:30
  • @IntelliJ Amiya, not possible at moment, this is on productions users without stats, probably in future I'll try add check google play services status – Tapa Save Oct 18 '17 at 12:33
  • @Mike M., yes, creating WebView throught inflating call exception like in your link too. Seems like 5.1 WebView implementation have bug. – Tapa Save Oct 18 '17 at 12:42
  • @Mike M., add 'Web View' to my strings not work for me, sorry. Need some manipulations with 'R' probably – Tapa Save Oct 18 '17 at 13:05
  • Yeah, now that I think about it, that shouldn't work, 'cause the identifier - which is just an `int` - that's compiled into that `WebView` component is going to be completely different than what an identifier in your app would end up being. Sorry 'bout that. I should've thought of that to begin with. – Mike M. Oct 19 '17 at 23:05

1 Answers1

9

This can be fixed by catching the error, and then creating the WebView with the Application context instead:

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());
}

Creating Views with the Application context is generally discouraged, but is an OK fallback here.

ronocod
  • 91
  • 1
  • 2
  • Did you find an actual solution for this ? I have a pretty big branch and on that branch webview on 5.1 doesn't work. Whereas my master branch works just fine. I have nothing changed in manifest/build gradle – Roudi Sep 17 '19 at 04:19
  • @ronocod Do you have any reference for this? – Manuel Oct 04 '19 at 13:17