超链接获取AndroidRuntimeException

时间:2015-04-30 13:58:12

标签: android

我在此应用中使用ViewPagerSherlockFragments并尝试在TextView中设置超链接。当我点击超链接时,应用程序崩溃了。此方法之前在其他应用程序中有效,但现在我得到AndroidRuntimeException。

public class FragmentNewsPage extends SherlockFragment {
/*
* 
**/
public void newsShow(String news) {

    mShareIntent.putExtra(Intent.EXTRA_TEXT, news);

    swipeRefreshLayout.setRefreshing(false);

    mMainText.setMovementMethod(LinkMovementMethod.getInstance());
    mMainText.setText(Html.fromHtml(mLink));
    mMainText.append("\n\n" + news);

    }
}

布局

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/newsText"
            android:textSize="20sp"
            android:textStyle="bold"
            android:clickable="true"
            android:textColor="@android:color/white"
            android:layout_gravity="center_horizontal"/>
    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

<LinearLayout
    android:id="@+id/linearLayout_newsPage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="false"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal"/>

</RelativeLayout>

记录错误

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
        at android.app.ContextImpl.startActivity(ContextImpl.java:1146)
        at android.app.ContextImpl.startActivity(ContextImpl.java:1133)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:328)
        at android.text.style.URLSpan.onClick(URLSpan.java:64)
        at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
        at android.widget.TextView.onTouchEvent(TextView.java:8200)
        at android.view.View.dispatchTouchEvent(View.java:7881)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2029)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2124)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1556)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2502)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2072)
        at android.view.View.dispatchPointerEvent(View.java:8072)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4392)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4263)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3809)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3859)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3828)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3935)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3836)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3992)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3809)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3859)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3828)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3836)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3809)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6124)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6104)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6058)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6254)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:5388)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.r

1 个答案:

答案 0 :(得分:1)

尝试从活动上下文中调用新活动,例如:

getActivity().startActivity(...);