句柄ClassCastException:无法将ViewComponentManager$FragmentConextWrapper强制转换为AppCompatActivity [英] Hilt ClassCastException: ViewComponentManager$FragmentContextWrapper cannot be cast to AppCompatActivity

查看:0
本文介绍了句柄ClassCastException:无法将ViewComponentManager$FragmentConextWrapper强制转换为AppCompatActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,当我在Adapter中单击viewHolder项时显示对话框片段

 SpecialRequestNotFoundBottomSheetDialog {
            requestItem?.specialRequestEntity?.id?.let { id -> onCancelReasonsSelected(id, it) }
        }.show(itemView.context as AppCompatActivity).supportFragmentManager)

最近我正在迁移到Hilt,但遇到了类强制转换异常,看起来像是Hilt包装了上下文,但我无法获取父活动来获取显示对话框所需的FragmentManager

推荐答案

我可以通过检查上下文类型并获取BaseContext找到解决此崩溃的方法。这是我现在使用的。我不知道是否有更好的方法来处理Hilt

private fun activityContext(): Context? {
    val context = itemView.context
    return if (context is ViewComponentManager.FragmentContextWrapper) {
        context.baseContext
    } else context
}

这篇关于句柄ClassCastException:无法将ViewComponentManager$FragmentConextWrapper强制转换为AppCompatActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆