Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crash on clicking select box in webview

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);
 }
}
like image 604
androgeek Avatar asked Apr 10 '12 08:04

androgeek


People also ask

How can I improve my WebView performance?

Enhance webView performance - disable the WebView cache to make WebView much faster. Supercharging the Android WebView - cache critical assets that drastically reduce the load time.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

What causes an application to crash?

Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.


1 Answers

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.

like image 95
Yoel Gluschnaider Avatar answered Nov 15 '22 05:11

Yoel Gluschnaider