我正从我的网站检索一些
HTML并将其放入TextView.此HTML可能包含指向网页,电子邮件等的锚标记.我正在尝试使这些链接可点击,以便当用户单击它时,默认浏览器会打开网页.我这样做是按照这个答案-
How do I make links in a TextView clickable?和其他几个在线来源的说明.
所以我的代码是这样的:
TextView para=new TextView(getApplicationContext()); LayoutParams lpfortext=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); lpfortext.setMargins(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)
解决方法
您似乎使用非活动上下文初始化para TextView对象,这需要在启动活动的Intent上设置Intent.FLAG_ACTIVITY_NEW_TASK.
这样可以解决问题
TextView para = new TextView(this);这是指活动背景