java - 在 Activity 中单击 URL 时出现 AndroidRuntimeException

标签 java android runtimeexception

<分区>

我有一个带有适配器的 Activity extends RecyclerView.Adapter<UserProfileAdapter.ViewHolder> .问题是,当用户单击其中一个列表项中的“自动链接”时,出现以下异常:

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

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

最佳答案

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

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

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

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

关于java - 在 Activity 中单击 URL 时出现 AndroidRuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45291242/

相关文章:

java - 持续的Android后台服务

java - 将驱动程序从 JTDS 更改为 MS SQL 驱动程序时出错

java - 将自定义异常消息存储在文件或类中

android - 无论如何要判断用户是否使用 facebook 安装了应用程序?

android - 添加圆角到约束布局会留下白色背景

android - getActivity() 调用导致 RuntimeException : Could not launch intent Intent act=android. intent.action.MAIN

java - 实际参数…无法通过方法调用转换转换为…

android更新项目——路径 "command not found"

session - 使用 Powershell 在远程计算机上运行脚本

java - 如何跟踪抛出异常的文件?