单击 Activity 中的 URL 时出现 AndroidRuntimeException

AndroidRuntimeException when clicking URL in Activity

我有一个 Activity 和一个 extends RecyclerView.Adapter<UserProfileAdapter.ViewHolder> 的适配器。问题是,当用户单击其中一个列表项中的 "autolink" 时,出现以下异常:

在我的 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;
    }

如何确保自动链接的文本不会产生此错误?由于没有与 link/intent 关联的代码,我该怎么办?

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

要解决它,您可以将 Activity 上下文而不是应用程序上下文传递给适配器(即:使用 this)。

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

良好做法:始终使用 Activity 上下文来处理 UI 元素。