5

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 HTML pages in a WebView. Everything looks nice and works OK, until you try to open a SELECT tag - boom, application crashes. Here's a trace stack, if this helps:

Thread [<1> main] (Suspended (exception WindowManager$BadTokenException)) AlertDialog(Dialog).show() line: 247
WebView$InvokeListBox.run() line: 7841
WebView$PrivateHandler(Handler).handleCallback(Message) line: 587
WebView$PrivateHandler(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 130 ActivityThread.main(String[]) line: 3859
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 840
ZygoteInit.main(String[]) line: 598 NativeStart.main(String[]) line: not available [native method]

(What does AlertDialog have to do with opening a select box is beyond me. And no, there's no script associated with the SELECT that opens an Alert).

Now, here's the best part. I'm testing the app on 2 devices, Samsung Galaxy S2 with OS 2.3.3, and Motorola RAZR XT910 with OS 2.3.5. On Samsung everything works wonderful. On RAZR, however, the above happens.

The pages are pretty heavy with CSS and JavaScript, but it's crashing even if everything is removed. In fact, an empty HTML with a single SELECT control still crashes. A few things mentioned in the question from year ago that I did try:

  • Removing all absolute and fixed positioned elements from the HTML (in fact I removed the entire CSS just to see if it has any effect - it doesn't).
  • Ensuring the SELECT is written by-the-book, no bogus attributes / tags.

Nothing helped.

Does anyone have the slightest clue as to what might be causing this?

Community
  • 1
  • 1
Igor K.
  • 750
  • 9
  • 25
  • In Android, dropdowns don't "drop down" (at least before ICS), they show the options in a list inside an AlertDialog, so that's where that comes from. Alas, I do not know why the issue happens. – dmon Feb 09 '12 at 15:00
  • You should post your HTML (or a sample) to see if anyone can spot something that might cause a crash. Have you tried with a simpler SELECT? – dmon Feb 09 '12 at 15:06
  • Looks simple enough, how much simpler of a control does a WebView need to function? I mean, the same WebView handled pretty heavy CSS and JS animation and what not, and everything was peachy. – Igor K. Feb 09 '12 at 15:09
  • Yeah that seems simple enough. I was honestly expecting strange characters or something of the sort :) . Does this work outside of the webview, e.g. in the browser app? – dmon Feb 09 '12 at 15:26
  • Everywhere, except that damned WebView on RAZR. – Igor K. Feb 09 '12 at 15:38
  • did you managed to resolve this issue ?thanks – Paul Feb 28 '12 at 13:15
  • Nope. I guess I will wait for the problem to magically disappear on it's own :) I did, however, think about another possible way to deal with this. I could, for example, bridge the select's "open" event to the native client, and it will show a list of options using native code, then pass the selection back to the HTML control. Not sure this will work, but I see no other options. – Igor K. Mar 01 '12 at 10:55

5 Answers5

5

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.

<LinearLayout
    xmlns: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">

    <WebView
            tools: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"

whit3hawks
  • 429
  • 7
  • 14
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.

Diffy
  • 2,339
  • 3
  • 25
  • 47
1

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.

Igor K.
  • 750
  • 9
  • 25
1

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.

OneGhana
  • 119
  • 5
0

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:

public static Webview initializeWebView(Context context)
{

  myWebView = new WebView();
  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.

Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30