7

我正在从我的站点中检索一些 HTML 并将其放入 TextView。此 HTML 可能包含指向网页、电子邮件等的锚标记。我试图使这些链接可点击,以便当用户单击它时,默认浏览器会打开网页。我是按照这个答案的说明来做的——如何使 TextView 中的链接可点击?和其他几个在线资源。

所以我的代码是这样的:

TextView para=new TextView(getApplicationContext());    
LayoutParams lpfortext=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lpfortext.setMargins(10, 10, 10, 10);
para.setText(Html.fromHtml(e.html()));
para.setMovementMethod(LinkMovementMethod.getInstance());
para.setLinksClickable(true); //error exists on removing this
para.setAutoLinkMask(Linkify.ALL);  //error exists on removing this
para.setLayoutParams(lpfortext);

但是当我实际点击应用程序中的链接时(在 ICS 手机上,尚未在模拟器上测试),我收到以下错误(堆栈跟踪的快照,最后是全文):

11-18 13:17:51.503: E/AndroidRuntime(6010): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-18 13:17:51.503: E/AndroidRuntime(6010):     at android.app.ContextImpl.startActivity(ContextImpl.java:1095)
11-18 13:17:51.503: E/AndroidRuntime(6010):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
11-18 13:17:51.503: E/AndroidRuntime(6010):     at android.text.style.URLSpan.onClick(URLSpan.java:62)
11-18 13:17:51.503: E/AndroidRuntime(6010):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)

任何想法可能导致错误以及如何解决它?

编辑:根据要求,完整跟踪如下——

11-18 13:45:09.741: E/AndroidRuntime(7946): FATAL EXCEPTION: main
11-18 13:45:09.741: E/AndroidRuntime(7946): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.app.ContextImpl.startActivity(ContextImpl.java:1095)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.text.style.URLSpan.onClick(URLSpan.java:62)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.widget.TextView.onTouchEvent(TextView.java:8835)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.View.dispatchTouchEvent(View.java:5579)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1734)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1982)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1441)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.app.Activity.dispatchTouchEvent(Activity.java:2421)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1930)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.View.dispatchPointerEvent(View.java:5759)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2970)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2529)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:869)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2538)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.os.Looper.loop(Looper.java:154)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at android.app.ActivityThread.main(ActivityThread.java:4945)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at java.lang.reflect.Method.invoke(Method.java:511)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-18 13:45:09.741: E/AndroidRuntime(7946):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

10

似乎您TextView使用非 Activity 上下文初始化 para 对象,这需要Intent.FLAG_ACTIVITY_NEW_TASKIntent启动 Activity 上进行设置。

这将解决问题

TextView para = new TextView(this);这指的是Activity上下文

于 2012-11-18T08:20:22.393 回答
10

如果您从 XML 为 TextView充气,请确保使用“getActivity().getLayoutInflater()”对包含 TextView 的布局进行充气。这为我解决了。

于 2014-01-14T21:52:35.247 回答