6

I have a simple WebView where JavaScript is enabled and WebChromeClient is used. A web content that I am loading has a select tag. When I click on it, the app crashes with following snippet of error:

 android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1351)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
    at android.content.res.Resources.getLayout(Resources.java:1165)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
    at org.chromium.content.browser.input.SelectPopupAdapter.getView(SelectPopupAdapter.java:56)

I found this material, but it didn't help and I couldn't find any solution for this problem. As far as I know, the app is crashed on Android 5 and 6 only. When select tag of HTML is pressed, it seems that Android tries to show its own Spinner. It uses Resources that causes the error somehow. I tried to create WebView progmatically, but it didn't help. Here is my code:

val webview = WebView(this)
    webview.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
    rootLayout.addView(webview)
    webview.webChromeClient = WebChromeClient()
    webview.settings.javaScriptEnabled = true
    webview.loadUrl(intent.getStringExtra(URL))

From libraries of Google, I use following:

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0-beta01'

My question is how to avoid this error? Maybe we can create custom WebView?

neo
  • 1,314
  • 2
  • 14
  • 34

3 Answers3

0

I reduced androidx.appcompat:appcompat:1.0.2 and it worked. But, sometimes downgrading appcompat version does not help. You should look into other libraries, that can cause the problem.

neo
  • 1,314
  • 2
  • 14
  • 34
0

I had this problem independent of the version of appcompat (I tried 1.0.2 and 1.2.0), and it was because I was instantiating the webview using the application context instead of the activity context

Quoting the webview documentation:

Note: WebView should always be instantiated with an Activity Context. If instantiated with an Application Context, WebView will be unable to provide several features, such as JavaScript dialogs and autofill.

David Bengoa
  • 127
  • 1
  • 7
0

Workaround for this is about loading a WebView instance with app's context, so that all resources could be pre-loaded with it:

Application() {
  override fun onCreate() {
    WebView(this).destroy()
  }
}

Workaround taken from https://issuetracker.google.com/issues/77246450

Damiancioo
  • 462
  • 3
  • 13