android.util.AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。

时间:2016-09-01 07:37:31

标签: android

public class MessagesAdapter extends BaseAdapter{

    public Context mContext;
    public LayoutInflater inflater;
    ArrayList<MessagesModelClass> mMessagesList = new ArrayList<MessagesModelClass>();
    Typeface fontMuseo;
    int pos;

    public MessagesAdapter(Context contxt,
                           ArrayList<MessagesModelClass> msgList) {
        this.mContext = contxt;
        this.mMessagesList = msgList;
        this.inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) {
            this.fontMuseo = Typeface.createFromAsset(mContext.getAssets(), "museo_sans_500.ttf");
        } else {
            this.fontMuseo = Typeface.createFromAsset(mContext.getAssets(), "Roboto-Regular.ttf");
        }
    }

    @Override
    public int getCount() {
        return mMessagesList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public static class ViewHolder {
        TextView heading, description,duration;
        RelativeLayout replyRL;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        pos = position;
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_messages, null);
            //holder.heading = (TextView) convertView.findViewById(R.id.heading_messages);
            holder.description = (TextView) convertView.findViewById(R.id.description_messages);
            holder.duration = (TextView) convertView.findViewById(R.id.time_duration);


            holder.description.setTypeface(fontMuseo);
            holder.duration.setTypeface(fontMuseo);



            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.description.setText(Html.fromHtml(mMessagesList.get(pos).getDescription()));
        holder.duration.setText(Html.fromHtml(mMessagesList.get(pos).getDuration()));

        holder.description.setAutoLinkMask(Linkify.WEB_URLS);

        return convertView;
    }

}

 <TextView
            android:id="@+id/description_messages"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Abcdefghijklmnopqrstuvwxyz"
            android:textColor="#000"
            android:autoLink="web"
            android:linksClickable="true"
            android:textColorLink="@color/blue"
            android:textSize="16sp"
            android:typeface="serif" />

我在我的android应用程序中的片段中使用了PullToRefreshListView。列表项包含Textview,具有Web链接。我没有将OnItemClickListener应用于PullToRefreshListview,我使用了android:autoLink =&#34; web&#34;和android:linksClickable =&#34; true&#34;在列表项TextView上。当我点击该Web链接时,它不会重定向到web.Its显示在Exception下面。

          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:1341)
             at android.app.ContextImpl.startActivity(ContextImpl.java:1328)
             at android.content.ContextWrapper.startActivity(ContextWrapper.java:322)
             at android.text.style.URLSpan.onClick(URLSpan.java:69)
             at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
             at android.widget.TextView.onTouchEvent(TextView.java:8273)
             at android.view.View.dispatchTouchEvent(View.java:8511)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at com.handmark.pulltorefresh.library.PullToRefreshListView$InternalListView.dispatchTouchEvent(PullToRefreshListView.java:307)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
             at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2239)
             at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2395)
             at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1739)
             at android.app.Activity.dispatchTouchEvent(Activity.java:2781)
             at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
             at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
             at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2356)
             at android.view.View.dispatchPointerEvent(View.java:8712)
             at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4536)
             at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4394)
             at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3915)
             at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3968)
             at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3934

1 个答案:

答案 0 :(得分:0)

旧的未回答的问题,但以防万一有人需要这个:

此错误是由使用应用程序 context.startActivity() 而不是活动上下文引起的。活动启动其他活动,但如果您想从应用程序上下文启动一个活动,您需要向意图添加新的任务标志。

OP 可能正在将应用上下文传递给原始帖子中的构造函数。