2

当我单击需要打开页面的文本视图时,我在数组中有一些 url,但我收到错误我的代码是

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)
4

5 回答 5

2

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

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

而是打电话

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

适配器在这两种情况下都可以正常工作,但链接仅在最后一种情况下有效。

于 2014-09-02T17:50:39.977 回答
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);
于 2012-12-20T18:06:19.583 回答
0

看来您是Activity从外部环境开始的Activity

在您启动活动的代码中的简单简短答案确保启动它的上下文是一个Activity否则确保将 Intent Flag 设置为Intent.FLAG_ACTIVITY_NEW_TASK

例子

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

mContext.startActivity(intent);
于 2012-12-20T17:12:02.203 回答
0

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

我觉得在您的 Logcat 中,您的问题的解决方案可以通过以下链接来解决:-您的 Logcat 问题

于 2012-12-20T17:04:18.497 回答
0

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

于 2017-07-24T23:14:53.510 回答