2

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?

4
  • dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show(); Commented Apr 16, 2015 at 11:48
  • I think you have to report on play store. Commented Apr 16, 2015 at 11:48
  • @Rah, and "dialog" is? Commented Apr 16, 2015 at 11:51
  • AlertDialog for onJsAlert. Commented Apr 16, 2015 at 12:31

3 Answers 3

5

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());
Sign up to request clarification or add additional context in comments.

1 Comment

This answer should be marked as accepted, not Pavan's one.
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

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.