【问题标题】:autolink not working on HTC - HtcLinkifyDispatcher自动链接在 HTC 上不起作用 - HtcLinkifyDispatcher
【发布时间】:2012-12-03 20:33:59
【问题描述】:

我在我的 Android 应用程序中的 TextView 上使用 android:autoLink 设置为 web 以启用可点击链接。但是,如果我在升级到 ICS 的 HTC Desire S 上运行它,当我点击其中一个链接时,会出现以下异常

android.content.ActivityNotFoundException: Unable to find explicit activity class 
  {com.htc.HtcLinkifyDispatcher/
  com.htc.HtcLinkifyDispatcher.HtcLinkifyDispatcherActivity}; 
  have you declared this activity in your AndroidManifest.xml?

    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1634)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1510)
    at android.app.Activity.startActivityFromChild(Activity.java:3519)
    at android.app.Activity.startActivityForResult(Activity.java:3271)
    at android.app.Activity.startActivity(Activity.java:3358)
    at woodsie.avalanche.info.InfoActivity.startActivity(InfoActivity.java:61)
    at android.text.style.LinkifyURLSpan.onClick(LinkifyURLSpan.java:73)

尝试注册HtcLinkifyDispatcherActivityget me nowhere 因为该类不在我的构建路径上。

【问题讨论】:

  • Issue 39682 似乎是该问题的相关错误报告。

标签: android android-4.0-ice-cream-sandwich autolink commonsware


【解决方案1】:

我发现与此相关的最佳文章是The Linkify Problem: The Detection and the Mitigation

但是,我没有尝试拦截和替换 URLSpan 类,而是降低了级别并覆盖了 TextView 的父 Activity 上的 startActivity()

@Override
public void startActivity(Intent intent) {
    try {
        /* First attempt at fixing an HTC broken by evil Apple patents. */
        if (intent.getComponent() != null 
                && ".HtcLinkifyDispatcherActivity".equals(intent.getComponent().getShortClassName()))
            intent.setComponent(null);
        super.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        /*
         * Probably an HTC broken by evil Apple patents. This is not perfect,
         * but better than crashing the whole application.
         */
        super.startActivity(Intent.createChooser(intent, null));
    }
}

Hacky 但简单,而且似乎有效。

【讨论】:

  • 只有当您以这种方式开始活动时,它才会起作用。由于任何Context 都可以启动活动,因此可以想象开发人员可以使用其他一些上下文(例如,泛滥互联网的getApplicationContext() 垃圾)。我也有点担心createChooser() 会一直忽略Intent 中已经指定的组件——我建议调用setComponent(null) 来清除它。
  • 因为我将是开发这个小应用程序的唯一开发人员,而且我不希望做任何比autolink 更出色的事情,这对我来说应该是一个合理的解决方法。
  • 我对它进行了更多尝试,在调用 startActivity() 之前调用 setComponent(null) 会使事情变得更好。它完全摆脱了邪恶的HtcLinkifyDispatcher,按预期启动默认浏览器而不显示选择器。我已经更新了答案中的代码示例,以反映我的超级安全实现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2020-08-27
  • 2018-11-12
相关资源
最近更新 更多