Android - Linkify问题

时间:2010-06-07 20:58:45

标签: android linkify

我似乎遇到了我在自定义适配器中使用的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);

我理解错误说的是什么,但我不确定如何修复它。有没有人有任何想法? 提前感谢您的帮助

1 个答案:

答案 0 :(得分:17)

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

不正确

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

正确的方式

CustomAdapter mAdapter = new CustomAdapter( 
                this,
                itemList);