Android——自动链接
有什么方法可以链接 ListView 中包含的特定 TextView 吗?我尝试使用 android:autoLink="all"
但这不起作用。我遇到了断章取义的错误。
还需要注意的是:ListView 是 ViewFlipper 中的第二个视图。
我也尝试过:
View mItemView = mAdapter.getView(2, null, null);
TextView infoText = (TextView) mItemView.findViewById(R.id.rowText2);
Linkify.addLinks(infoText, Linkify.ALL);
在适配器绑定到ListView并且切换视图之后。运气不好。
这是堆栈跟踪:
06-03 21:19:25.180: ERROR/AndroidRuntime(1214): Uncaught handler: thread main exiting due to uncaught exception
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): 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-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.app.ApplicationContext.startActivity(ApplicationContext.java:550)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.content.ContextWrapper.startActivity(ContextWrapper.java:248)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.text.style.URLSpan.onClick(URLSpan.java:62)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.widget.TextView.onTouchEvent(TextView.java:6560)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.View.dispatchTouchEvent(View.java:3709)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.os.Looper.loop(Looper.java:123)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at java.lang.reflect.Method.invoke(Method.java:521)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at dalvik.system.NativeStart.main(Native Method)
有什么想法吗?
提前致谢!!!
Is there any way to Linkify a specific TextView that is contained within a ListView? I tried using android:autoLink="all"
but that didn't work. I was getting an out of context error.
Important also to note: the ListView is my second view in the ViewFlipper.
I have also tried:
View mItemView = mAdapter.getView(2, null, null);
TextView infoText = (TextView) mItemView.findViewById(R.id.rowText2);
Linkify.addLinks(infoText, Linkify.ALL);
Right after the adapter was bound to the ListView and the View was switched. No luck.
Here is the stack trace:
06-03 21:19:25.180: ERROR/AndroidRuntime(1214): Uncaught handler: thread main exiting due to uncaught exception
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): 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-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.app.ApplicationContext.startActivity(ApplicationContext.java:550)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.content.ContextWrapper.startActivity(ContextWrapper.java:248)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.text.style.URLSpan.onClick(URLSpan.java:62)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.widget.TextView.onTouchEvent(TextView.java:6560)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.View.dispatchTouchEvent(View.java:3709)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.os.Looper.loop(Looper.java:123)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at java.lang.reflect.Method.invoke(Method.java:521)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 21:19:25.219: ERROR/AndroidRuntime(1214): at dalvik.system.NativeStart.main(Native Method)
Any Ideas?
Thanks in advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
http://www.anddev.org/view -layout-resource-problems-f27/linkify-problem-t14779.html
基本上,当你无意(从链接中窃取)时,不要使用
getApplicationContext()
好(linkify 有效)
不好(linkify 在运行时失败:从外部调用 startActivity()...)
This worked for me:
http://www.anddev.org/view-layout-resource-problems-f27/linkify-problem-t14779.html
Basically, don't use
getApplicationContext()
when you don't mean it (stealing from the link)GOOD (linkify works)
BAD (linkify fails at run with: Calling startActivity() from outside...)
该堆栈跟踪抱怨您在活动上下文之外调用了 startActivity() 而没有设置特定标志。它似乎与您的 Linkify 无关。
That stack trace is complaining about the fact that you have called startActivity() outside of an activity context without setting a particular flag. It does not seem to relate to your Linkify.
我遇到了同样的错误。并与之斗争了大约 4 天..之后我得到了解决方案。
我会告诉你我做了什么......
当你在类的 onCreate 中使用 Linkify 时,它会完美地工作。当您创建一个方法并尝试在其中调用 Linkify 时,它不起作用并给出相同的错误。
如果您必须调用不同的方法,那么请将 onCreate 的上下文传递给它并用它创建 Textviews 或其他...我猜这似乎与第一个答案有点一致...
Rajesh
I got the same error. and struggled with it for about 4 days.. after which i got the solution.
I will telll you what i did...
when you use Linkify in the onCreate of a class then it works perfectly. when you create a method and try to call the Linkify in it then it does not work and gives you the same error.
if you have to call a different method then please pass you context of that onCreate to it and create the Textviews or others with it.... it seems in accordance with the first answer a little i guess...
Rajesh