AndroidRuntimeException当单击超链接在对话框中显示在第一次运行单击、超链接、对话框中、AndroidRuntimeException

2023-09-12 05:08:37 作者:你的小可爱已到货

我的主要活动检查,看它是否是第一次的用户已经运行在其onCreate方法的应用程序。在对话框中,这是一个web视图,有一个为回发送电子邮件给我一个超链接。当用户点击链接,不过,我发现了以下异常:

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 android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

这是在code,创建对话框:

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();
}

在HTML中(资产内)的链接是一个典型的mailto链接:

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>

由于对话是建立与AlertDialog.Builder并没有一个明确的意图(而不是实际调用startActivity明确),我无法[就我所知]添加标记为异常消息的状态。我发现一些职位在这里有一个类似的异常,但没有一个是相同的或已修补程序解决了我的问题。在此先感谢您的帮助。

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);
 
精彩推荐