0

我正在尝试一些简单的东西。在 AlertDialog 消息中包含超链接。我可以包含超链接,并根据需要将其绘制为超链接。但是当点击它时,应用程序崩溃了:

ActivityMain.java

AlertDialog ad = new AlertDialog.Builder(this).create();
        ad.setMessage(Html.fromHtml("<a href=\\\"http://www.google.com\\\">Check this link out</a>));
        ad.setButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        ad.show();
        TextView textView = (TextView) ad.findViewById(android.R.id.message);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

在我的AndroidManifext.xml我添加了意图过滤器android.intent.action.VIEW,但它仍然崩溃。

        <activity
            android:name="org.app.test"
            android:screenOrientation="portrait"
            android:label="@string/app_name"
            android:theme="@style/AppTheme"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Logcat 错误信息

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=\"http://www.google.com\" (has extras) }
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
            at android.app.Activity.startActivityForResult(Activity.java:3511)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivityForResult(Native Method)
            at android.app.Activity.startActivityForResult(Activity.java:3472)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivityForResult(Native Method)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
            at android.app.Activity.startActivity(Activity.java:3714)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivity(Native Method)
            at android.app.Activity.startActivity(Activity.java:3682)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivity(Native Method)
            at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.content.ContextWrapper.startActivity(Native Method)
            at android.text.style.URLSpan.onClick(URLSpan.java:62)
            at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
            at android.widget.TextView.onTouchEvent(TextView.java:9180)
            at android.view.View.dispatchTouchEvent(View.java:8107)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2280)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1615)
            at android.app.Dialog.dispatchTouchEvent(Dialog.java:757)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
            at android.view.View.dispatchPointerEvent(View.java:8315)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4596)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4464)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4156)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
            at android.view.ViewRootImpl$Async

怎么了?

4

2 回答 2

0

You need to make a call to the users web browser to open it or a call to webview to open in app. You don't have anything coded to cover that.

WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl(www.google.com);

The above is for in app. For pulling the users web client:

getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activiy activity = this;
webview.setWebChromeClient(new WebChromeClient() {


public void onProgressChanged(WebView view, int progress) {
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String  description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description,     
    Toast.LENGTH_SHORT).show();
       }
     });
    webview.loadUrl("www.google.com");
于 2015-08-24T22:45:04.217 回答
0

如果我使用 Linkify,则可以毫无错误地单击该链接并在 Web 浏览器中打开该链接。

String msg = "<a href=\"http://www.google.com\">Check this link out</a>";

final SpannableString s = new SpannableString(msg);
Linkify.addLinks(s, Linkify.ALL);

AlertDialog ad = new AlertDialog.Builder(this)
                        .setMessage(s)
                        .setTitle("Click a link in AlertDialog")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .create();

TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());

几乎是我想要实现的目标......但是链接没有正确呈现...... :(

这是它打印到我的 AlertDialog 中的结果:

<a href=\" http://www.google.com \">查看此链接</a>

当然它应该在没有 HTML 标记的情况下显示它,就像这样 ->检查这个链接...


因此,这是在 AlertDialogs 中获得格式正确的超链接而没有任何错误的最终解决方案:

    String msg = "Check this link out, and I will send you to Google.";

    AlertDialog ad = new AlertDialog.Builder(this)
            .setTitle("Open a Hyperlink inside AlertDialog")
            .setMessage(msg)
            .setIcon(android.R.drawable.ic_dialog_info)
            .create();

    ad.show();

    TextView message = (TextView) ad.findViewById(android.R.id.message);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    addLinks(message , "Check this link out", "http://www.google.com");

我们唯一需要做的就是在我们的类中添加一个小的新函数addLinks(Textview, String, String)。否则当我们只使用 Linkify.addLinks(TextView textView, Pattern pattern, String scheme) 时生成的 URL 将是错误的。

public static void addLinks(TextView textView, String linkThis, String toThis)
    {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        Linkify.addLinks(textView, pattern, scheme, new Linkify.MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new Linkify.TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

所以,这让它真的很舒服!:)

于 2015-08-24T23:52:58.393 回答