Android - Linkify 问题

发布于 2024-09-04 10:11:15 字数 2905 浏览 5 评论 0原文

我在自定义适配器中使用的 linkify 似乎遇到了问题。由于某种原因,当我单击其中一个链接时,我收到以下堆栈跟踪:

06-07 20:49:34.696: ERROR/AndroidRuntime(813): Uncaught handler: thread main exiting due to uncaught exception
06-07 20:49:34.745: ERROR/AndroidRuntime(813): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.app.ApplicationContext.startActivity(ApplicationContext.java:550)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:248)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.text.style.URLSpan.onClick(URLSpan.java:62)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.widget.TextView.onTouchEvent(TextView.java:6560)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.View.dispatchTouchEvent(View.java:3709)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)

这是调用它的代码

    TextView bot = new TextView( c );
    bot.setText(li.getBottomText());
    bot.setTextColor(Color.BLACK);
    bot.setTextSize(12);
    bot.setPadding(50, 35, 0, 10);
    Linkify.addLinks(bot, Linkify.ALL);
    rL.addView(bot,ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

我明白错误的含义,但我不确定如何修复它。有人有什么想法吗? 预先感谢您的帮助

I seem to be having trouble with the linkify I am using in my Custom Adapter. For some reason I recieve the following stack track when I click on one of the links:

06-07 20:49:34.696: ERROR/AndroidRuntime(813): Uncaught handler: thread main exiting due to uncaught exception
06-07 20:49:34.745: ERROR/AndroidRuntime(813): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.app.ApplicationContext.startActivity(ApplicationContext.java:550)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:248)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.text.style.URLSpan.onClick(URLSpan.java:62)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.widget.TextView.onTouchEvent(TextView.java:6560)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.View.dispatchTouchEvent(View.java:3709)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-07 20:49:34.745: ERROR/AndroidRuntime(813):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)

Here is the code that is calling it:

    TextView bot = new TextView( c );
    bot.setText(li.getBottomText());
    bot.setTextColor(Color.BLACK);
    bot.setTextSize(12);
    bot.setPadding(50, 35, 0, 10);
    Linkify.addLinks(bot, Linkify.ALL);
    rL.addView(bot,ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

I understand what the error is saying but I am not sure how to fix it. Does anyone have any ideas? Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2024-09-11 10:11:15

事实证明这个问题的答案比我最初想象的要简单得多。问题是,当我将上下文传递给自定义适配器时,我传递的是 getApplicationContext() ,这与使用 this 标识符不同。

不正确的方式

Context mContext = getApplicationContext();
CustomAdapter mAdapter = new CustomAdapter( 
                mContext,
                itemList); 

正确的方式

CustomAdapter mAdapter = new CustomAdapter( 
                this,
                itemList); 

Turns out the answer to this is a lot simpler than I had originally thought. The problem was that when I was passing the context to my custom adapter I was passing the getApplicationContext() which is not the same as using the this identifier.

INCORRECT WAY:

Context mContext = getApplicationContext();
CustomAdapter mAdapter = new CustomAdapter( 
                mContext,
                itemList); 

CORRECT WAY:

CustomAdapter mAdapter = new CustomAdapter( 
                this,
                itemList); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文