6

I am using a webview in my application. Webview is created using application context . App crashes on clicking any select box

04-10 14:19:14.502: E/AndroidRuntime(12628): Uncaught handler: thread main exiting due to uncaught exception
04-10 14:19:14.542: E/AndroidRuntime(12628): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.view.ViewRoot.setView(ViewRoot.java:476)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.app.Dialog.show(Dialog.java:239)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.webkit.WebView$InvokeListBox.run(WebView.java:9509)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.os.Handler.handleCallback(Handler.java:609)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.os.Looper.loop(Looper.java:123)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at android.app.ActivityThread.main(ActivityThread.java:4595)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at java.lang.reflect.Method.invokeNative(Native Method)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at java.lang.reflect.Method.invoke(Method.java:521)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-10 14:19:14.542: E/AndroidRuntime(12628):    at dalvik.system.NativeStart.main(Native Method)

I am well aware that this question has been asked already many times, but I am yet to find any working solution. This problem will get solved if I use activity context but I have to use application context due to some other concerns. For other alerts and dialogs I have handled them by overriding onJsAlert() in webchromeclient, but I am not able to find how I can intercept this in my webview and create my own selector dialog.

Any kind of suggestions/help is much appreciated.

Adding a dummy sample of my webview implementation

class MyWebView extends WebView
{

 MyWebView(Context context)
 {

    super(context.getApplicationContext());
    setWebChromeClient(myWebChromeClient);
    setWebViewClient(myWebViewClient);
 }
}
androgeek
  • 276
  • 2
  • 9
  • what have you kept in extends is it Activity or WebView? – Bhavin Apr 10 '12 at 08:59
  • its webview . Its getting called from a different activity.I am creating webview at runtime (I am not using any layout for it) using application context – androgeek Apr 10 '12 at 09:05
  • if you are aware of such situation, then you must use Activity context rather than Application, since you are trying to do some UI stuff without the UI context which should be of Activity. – Neeraj Nama Apr 10 '12 at 09:06
  • 2
    @neeraj: I understand that, to avoid some memory leak problems I am pushed to use application context in my implementation.This leads to crash for any dialog getting created from html. I handled alert box by intercepting them in onJSAlert() but I am not able to intercept and handle select box for html – androgeek Apr 10 '12 at 09:19
  • I think the problem is from WebChromeClient, take a look at the answer http://stackoverflow.com/a/12638789/505530 – Bolton Sep 29 '12 at 06:11

2 Answers2

3

This is because you pass the application context instead of an activity context when you create the WebView. To show a select box in Android from the WebView, the WebView uses the context given to it in its constructor and opens a native single choice selector. To show this selector dialog it needs an Activity context and not an Application's one. Change the line:

super(context.getApplicationContext());

With this line:

super(context);

You also need to make sure you use the web view in the activity from which you created it (there is no sense in using it on a different activity). If you need the web view in a different activity, just create one using the designated activity's context.

Yoel Gluschnaider
  • 1,785
  • 1
  • 16
  • 20
0

I would like you to Use extends Activity in such case.

just take a reference from here.

And if you want to use webView in addition to this then, i have writtend following Code for that.

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
Bhavin
  • 6,020
  • 4
  • 24
  • 26
  • Thanks for your support, but problem is I am creating my webview for a library which can be used by different application activity, so this is the approach which I have to follow in any case – androgeek Apr 10 '12 at 09:20