2017-07-24 28 views
0

我有一个适配器活动extends RecyclerView.Adapter<UserProfileAdapter.ViewHolder>。问题是,当用户点击列表中的项目之一的“自动连接”,我得到以下异常:AndroidRuntimeException当在活动中单击URL时

在我UserProfileActivity,我实例化我的适配器像他:

UserProfileAdapter adapter = new UserProfileAdapter(getApplicationContext(), posts); 

在适配器,我找回这样的背景下:

private Context mContext; 
    private List<ParseObject> mYeets; 
    private UserProfileAdapter adapter; 

    public UserProfileAdapter(Context context, List<ParseObject> yeets) { 
     super(); 

     this.mYeets = yeets; 
     this.mContext = context; 
     this.adapter = this; 
    } 

enter image description here

我怎样才能确保自动链接文本不会产生这个错误?由于没有与链接/意图关联的代码,我甚至可以做什么?

+1

你截图的消息中有哪些不清楚? –

+0

事实上,没有实际的意图启动链接。它是一个Recycler列表项中的“自动链接”文本。所以我只是不能去为这个意图添加一个标志。 – santafebound

回答

2

android.util.AndroidRuntimeException被抛出是因为在您的应用程序中使用应用程序上下文来启动没有标记FLAG_ACTIVITY_NEW_TASK的活动。 Android Runtime不允许这种操作,因为没有Stack来添加这个新的Activity,并且FLAG_ACTIVITY_NEW_TASK标志对于在应用程序中创建新的Stack很重要。

要解决该问题,您可以将活动上下文传递给Adapter而不是Application Context(即:使用this)。

例如为:UserProfileAdapter adapter = new UserProfileAdapter(this, posts);

良好实践:始终使用活动上下文与UI元素的工作。

相关问题