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 ponent. 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 ponent and checked. Alerts are being shown fine. If this is an issue, where can I report it?
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 ponent. 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 ponent and checked. Alerts are being shown fine. If this is an issue, where can I report it?
Share Improve this question asked Apr 16, 2015 at 11:43 Pavan AndhukuriPavan Andhukuri 1,6173 gold badges27 silver badges49 bronze badges 4- dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show(); – Rahul Commented Apr 16, 2015 at 11:48
- I think you have to report on play store. – Hareshkumar Chhelana Commented Apr 16, 2015 at 11:48
- @Rah, and "dialog" is? – Pavan Andhukuri Commented Apr 16, 2015 at 11:51
- AlertDialog for onJsAlert. – Rahul Commented Apr 16, 2015 at 12:31
3 Answers
Reset to default 4To 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());
Here is the answer from JavaScript alert not working in Android WebView Check this link , and last ment , You have to use WebChromeClient for your purpose.
http://code.google./p/android/issues/detail?id=752
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./p/chromium/issues/detail?id=478204