设置自动链接属性

时间:2012-12-20 16:12:01

标签: android android-layout

我在数组中有一些网址,当我点击它需要打开页面的文本视图但我得到错误我的代码是

LinearLayout l = new LinearLayout(this);
    l.setOrientation(LinearLayout.VERTICAL);
    String[] textArray={"www.google.co.in","www.gmail.com","www.facebook.com"};
    int length=textArray.length;
    LinearLayout layout = new LinearLayout(this);
    setContentView(layout);
    layout.setOrientation(LinearLayout.VERTICAL);        
    for(int i=0;i<length;i++)
    {
        TextView tv=new TextView(getApplicationContext());
        tv.setText(textArray[i]);
        layout.addView(tv);
       Linkify.addLinks(tv, Linkify.WEB_URLS);
       tv.setMovementMethod(LinkMovementMethod.getInstance())
  

LogCat输出

12-20 16:08:38.623: E/InputEventReceiver(2170): Exception dispatching input event.
12-20 16:08:38.623: E/MessageQueue-JNI(2170): Exception in MessageQueue callback: handleReceiveCallback
12-20 16:08:38.804: E/MessageQueue-JNI(2170): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.app.ContextImpl.startActivity(ContextImpl.java:944)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.app.ContextImpl.startActivity(ContextImpl.java:931)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.text.style.URLSpan.onClick(URLSpan.java:62)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.widget.TextView.onTouchEvent(TextView.java:7397)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.View.dispatchTouchEvent(View.java:7239)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.View.dispatchPointerEvent(View.java:7419)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.os.MessageQueue.nativePollOnce(Native Method)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.os.MessageQueue.next(MessageQueue.java:125)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.os.Looper.loop(Looper.java:124)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at android.app.ActivityThread.main(ActivityThread.java:5039)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at java.lang.reflect.Method.invokeNative(Native Method)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at java.lang.reflect.Method.invoke(Method.java:511)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-20 16:08:38.804: E/MessageQueue-JNI(2170):   at dalvik.system.NativeStart.main(Native Method)
12-20 16:08:38.804: D/AndroidRuntime(2170): Shutting down VM
12-20 16:08:38.813: W/dalvikvm(2170): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-20 16:08:38.913: E/AndroidRuntime(2170): FATAL EXCEPTION: main
12-20 16:08:38.913: E/AndroidRuntime(2170): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.app.ContextImpl.startActivity(ContextImpl.java:944)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.app.ContextImpl.startActivity(ContextImpl.java:931)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.text.style.URLSpan.onClick(URLSpan.java:62)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.widget.TextView.onTouchEvent(TextView.java:7397)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.View.dispatchTouchEvent(View.java:7239)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.View.dispatchPointerEvent(View.java:7419)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.os.MessageQueue.nativePollOnce(Native Method)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.os.MessageQueue.next(MessageQueue.java:125)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.os.Looper.loop(Looper.java:124)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at java.lang.reflect.Method.invoke(Method.java:511)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-20 16:08:38.913: E/AndroidRuntime(2170):     at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:2)

此外:如果您在片段中的适配器中显示链接,请不要像这样创建

    adapter = new ListAdapter(getActivity().getApplicationContext(),mStrings,dispWidth,dispHeight);

而是打电话

    adapter = new ListAdapter(getActivity(),mStrings,dispWidth,dispHeight);

适配器在两种情况下都能正常工作,但链接仅适用于最后一种。

答案 1 :(得分:0)

您好我认为您应该使用WebView打开任何链接。您可以从这里获得一个如何处理WebView的简单示例。链接如下: - WebView Example。 我希望这对你有很大的帮助。

我觉得在你的Logcat中你的问题的解决方案可以通过以下链接来实现: - Your Logcat Issue

答案 2 :(得分:0)

您似乎从Activity

的上下文开始Activity

您开始活动的代码中的简单简短答案确保启动它的上下文是Activity,否则请务必将意图标记设置为Intent.FLAG_ACTIVITY_NEW_TASK

示例

Intent intent = new Intent(mContext, SomeActivity.class);
intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
..
...
..

mContext.startActivity(intent);

答案 3 :(得分:0)

检查Nammari的响应或者是否在getView()方法中包含该代码以避免该异常

  

“从Activity上下文外部调用startActivity()需要   FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?“

可能你需要:

LinearLayout l = new LinearLayout(parent.getContext());
...
...
LinearLayout layout = new LinearLayout(parent.getContext());

而不是

LinearLayout l = new LinearLayout(this);
...
...
LinearLayout layout = new LinearLayout(this);

答案 4 :(得分:0)

LinearLayout l = new LinearLayout(this);更改为LinearLayout l = new LinearLayout(getParent.getApplicationContext());