Android WebView crashes when clicking on a link

android, webview

Solution

The problem was that my `webview` was initialized with wrong context...

i used:

`WebView w = new WebView(this.getApplicationContext())`

which crashes...

the correct `context` is the `activity` itself

`WebView w = new WebView(this)`

Problem

I have some `WebView` widgets inside my `Activity`. I use `loadData()` to set the content, and this html contains a link. Some of my `WebViews` work okay, when I click the link, the web browser is started in a new window, but some make my app crash when I click on a link. ``` 10-13 08:45:24.257: ERROR/AndroidRuntime(751): Uncaught handler: thread main exiting due to uncaught exception 10-13 08:45:24.308: ERROR/AndroidRuntime(751): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.app.ApplicationContext.startActivity(ApplicationContext.java:627) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.content.ContextWrapper.startActivity(ContextWrapper.java:236) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:185) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:277) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.os.Handler.dispatchMessage(Handler.java:99) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.os.Looper.loop(Looper.java:123) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at android.app.ActivityThread.main(ActivityThread.java:3948) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at java.lang.reflect.Method.invokeNative(Native Method) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at java.lang.reflect.Method.invoke(Method.java:521) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 10-13 08:45:24.308: ERROR/AndroidRuntime(751): at dalvik.system.NativeStart.main(Native Method) ``` There seems to be a problem with the `Intent` that the `WebView` fires when I click the URL, but I have no control of this `Intent`, I think. Does anybody have any idea? I use this `AsyncTask` to set the content in the `WebView`, and it works fine: ``` public class SetAdTask extends AsyncTask<Void, Void, String>{ private Main main; private WebView webView; public SetAdTask(Main main, WebView webView){ this.main = main; this.webView = webView; } protected void onPreExecute() { Main.Log("onPreExecute()"); } protected String doInBackground(Void... params) { Main.Log("doInBackground()"); return main.getRestClient().getAdCode(); } protected void onPostExecute(String result) { Main.Log("onPostExecute()"); String html = "<html><head><title>ad</title></head><body>"; html += "<div style=\"color:grey;font-size:12px;\">ADVERTISEMENT<br/>"; html += result; html += "</body></html>"; webView.loadData(html, "text/html", "latin-1"); } } ```

Original source