1

We built a hybrid application using android webview and I use my phone (LG Nexus 4) to test it. The android application used to show JavaScript alerts from the website properly earlier but lately it has stopped showing the same.

It is throwing the following error whenever an alert is to be thrown from the application and nothing is shown.

 Cannot create a dialog, the WebView context is not an Activity

I am suspecting that this behaviour has started after google has recently updated the WebView component. I am right now running on Android 5.0.1 and webview version is 40 (1832189-arm)

I will try rolling back the updates and check but I can't ask all the users to do it. Are there any other issues reported similar to this? Is there a solution?

Update I have uninstalled the updates to the WebView component and checked. Alerts are being shown fine. If this is an issue, where can I report it?

Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49

3 Answers3

3

To get JavaScript Alert to work on Android WebView add below line of code:

mWebView.setWebChromeClient(new WebChromeClient());

After following Line of code:

webSettings.setJavaScriptEnabled(true);

Finally it Should look like:

webSettings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
Shashank Pujari
  • 159
  • 1
  • 7
1

Here is the answer from JavaScript alert not working in Android WebView Check this link , and last comment , You have to use WebChromeClient for your purpose.

http://code.google.com/p/android/issues/detail?id=752

Community
  • 1
  • 1
Lumi Lu
  • 3,289
  • 1
  • 11
  • 21
  • I have added the WebChromeClient. That is how the alerts were popping up in the earlier versions of WebView. But it is not working now with the upgraded WebView. Thanks. – Pavan Andhukuri Apr 16 '15 at 12:17
1

This seems to be a bug introduced into build 40 of WebView. It is fixed in version 42 as per the issue I've logged.

https://code.google.com/p/chromium/issues/detail?id=478204

Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49