J'ai quelques WebView
widgets à l'intérieur de mon Activity
. J'utilise loadData()
pour définir le contenu, et ce code HTML contient un lien. Certains de mes WebViews
fonctionnent correctement, lorsque je clique sur le lien, le navigateur Web est démarré dans une nouvelle fenêtre, mais certains font planter mon application lorsque je clique sur un lien.Android WebView se bloque en cliquant sur un lien
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)
Il semble être un problème avec le Intent
que les WebView
se déclenche lorsque je clique l'URL, mais je n'ai pas le contrôle de cette Intent
, je pense.
Est-ce que quelqu'un a une idée?
J'utilise cette AsyncTask
pour définir le contenu de la WebView
, et il fonctionne très bien:
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");
}
}
Oh, j'ai trouvé le problème ... J'ai lancé le webView avec un mauvais contexte ... j'ai utilisé viewView = new WebView (main.getApplicationContext()) ... On dirait que le webview rend OK, avec ce contexte, mais ne peut pas gérer les intentions correctement ... T –
Vous pouvez répondre à votre propre question, vous savez. –