My main activity checks to see if it's the first time the user has run the app in its onCreate method. In the dialog, which is a WebView, there's a hyperlink for sending email back to me. When the user clicks on the link, though, I'm getting the following exception:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:651)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:235)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:330)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4717)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
The Activity causing the exception is set up like this in the manifest file:
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent android:action="android.intent.action.VIEW" />
<intent-filter>
<action androi开发者_C百科d:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is the code that creates the dialog:
private Dialog createFirstRunDialog() {
LayoutInflater inflator = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflator.inflate(R.layout.tutorial, (ViewGroup)findViewById(R.id.tutorialMain));
WebView webView = (WebView)layout.findViewById(R.id.tutorialBody);
webView.loadUrl(TUTORIAL_HTML_FILE);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setView(layout)
.setCancelable(true)
.setIcon(R.drawable.icon)
.setTitle(R.string.tutorial_title)
.setNegativeButton(R.string.tutorial_doneButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismissDialog(TUTORIAL_DIALOG_ID);
}
});
return dialogBuilder.create();
}
The link within the html (inside an asset) is a typical mailto link:
<a href="mailto:support@myapp.com?subject=MyApp feature request">Tell us</a>
As the dialog is built with an AlertDialog.Builder and not an explicit Intent (not actually calling startActivity explicitly), I'm unable [as far as I'm aware] to add the flag as the exception message states. I found some posts here with a similar exception, but none were the same or had fixes that solved my issue. Thanks in advance for your help.
You have to intercept the link and handle it yourself.
WebView webView = (WebView)layout.findViewById(R.id.tutorialBody);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("mailto:")) {
MailTo mt = MailTo.parse(url);
Intent intent = new Intent();
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {mt.getTo()});
intent.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject());
startActivity(Intent.createChooser(intent, "Email ..."));
return true;
}
return false;
}
});
webView.loadUrl(TUTORIAL_HTML_FILE);
精彩评论