Skip to content Skip to sidebar Skip to footer

Trying To Open Select Tag In Android Webview Crashes The Application

A similar question was asked about a year ago, and wasn't quite resolved, but I'm gonna try my luck anyhow, maybe someone knows this. I have this application that runs a couple of

Solution 1:

Actually what you have done is that you have passed the Application Context to the webview. A SELECT tag basically displays its options using Android's native AlertDialog which needs an Activity Context.

To fix the issue you can pass the Activity Context through the layout(XML) file as shown below.

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:fitsSystemWindows="true"tools:context="com.myApp.Activities.WebViewActivity"><WebViewtools:context="com.myApp.Activities.WebViewActivity"android:layout_width="match_parent"android:id="@+id/webView"android:layout_height="match_parent"/></LinearLayout>

tools:context="com.myApp.Activities.WebViewActivity"

Solution 2:

It crashes because you have given ApplicationContext to the webview. When a SELECT tag is clicked, Android internally displays its options using a native AlertDialog. Webview must be created with an Activity context because AlertDialog instance needs an Activity context.

Solution 3:

In my case, i was using androidx and all i did was to update all gradle implementations to the latest. And i changed my compileSDK version to 29 as per the current minimum for November 2020 onwards per playstore guidelines. And it worked fine for me.

Solution 4:

After looking all over the web for over a month, I've given up and relayed all SELECTs to native code.

Replace the SELECT with something that looks like SELECT, bind click handler that launches a method on JavascriptInterface and pass the SELECTs option values to it, let the method open a Dialog with a RadioGroup, fill the group with RadioButtons representing the options. When selected, resolve the index and pass it back to the JavaScript (the SELECT instance that launched the process should be saved in some var first).

This is ugly as hell, but sadly nothing else seemed to work.

Solution 5:

Late to the game, but I've been reading for half a working day, and trying different solutions for several working days, and came back to this thread over and over again. I had a fragment with a Webview inside it, and android versions prior Oreo (28) crashed when selecting a select in HTML while 28+ just ignored it.

It's what Diffy said and Igor_K said (although Diffy's solution didn't work): it's the wrong context. What made me solve it was this thread. I will copy-paste the answer below:

Heaps of love to Manish Sharma for this answer.

To pass new context to webview you can create a method to initialize webview, passing an argument of Context like shown below:

publicstaticWebviewinitializeWebView(Context context)
{

  myWebView = newWebView();
  return myWebView;

}

And after this you can call this method whereever you want and whenever you want. You can call this as shown below:

myWebView = initializeWebView(YourActivityName.this);
//this way whatever Context you will pass your webview will be initialized that way//for example you can also pass getApplicationContext() as an Argument
myWebView = initializeWebView(getApplicationContext());
//or
myWebView = initializeWebView(customContext);

this customContext can be any context that is inherited from other context that you wanted to use.

Post a Comment for "Trying To Open Select Tag In Android Webview Crashes The Application"