gpt4 book ai didi

android.content.ActivityNotFoundException 当链接不包含 http

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:55 27 4
gpt4 key购买 nike

我的应用程序允许用户在使用有限的 HTML 的同时向其他用户键入消息。我允许的其中一件事是使用超链接。

例子:

<a href="www.google.com">Google</a>

我正在填充 TextView通过以下方法:

txtview.setMovementMethod(LinkMovementMethod.getInstance());
txtview.setText(Html.fromHtml(items.get(position).getBody()));

如果用户创建一个没有前缀 http 的超链接到 url,应用程序崩溃并出现以下异常:

FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)

如果url以http为前缀,一切正常。

例子:

<a href="http://www.google.com">Google</a>

如何防止这种情况发生?

最佳答案

问题是 Html.fromHtml() 创建了 URLSpan嵌入 URL 的实例,并且此类“盲目地”使用提供的 URL 调用 startActivity()。只要 URL 与任何已注册的 Activity 不匹配,就会崩溃。

问题在this CommonsWare post 中得到了很好的解释。 .那里的解决方案/示例覆盖 onClick() 并处理 ActivityNotFoundException 以防止崩溃。

如果您想要做的是对链接更加宽容,您可以改写 getURL(),例如如下所示:

    @Override
public String getURL()
{
String url = super.getURL();
if (!url.toLowerCase().startsWith("http"))
url = "http://" + url;

return url;
}

请注意,这是一个非常粗略的示例(例如,它不考虑“https”链接)——根据需要进行改进!

关于android.content.ActivityNotFoundException 当链接不包含 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26617738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com