Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 362
Igor K. Avatar asked Feb 09 '12 14:02

Igor K.


1 Answers

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"

like image 193
whit3hawks Avatar answered Oct 18 '22 23:10

whit3hawks