You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Fragment注册Listener时出现ClassCastException问题求助

问题分析与解决:Fragment转接口时的ClassCastException错误

我定义了R2OnClickFullListener接口,在Rove2LiveVideoFragment的onAttach方法中尝试将关联Activity强转为该接口实例,用于Fragment向Activity传值。Rove2MainActivity已经实现了该接口,但运行时抛出java.lang.ClassCastException,错误提示要求实现MyListFragment.OnItemSelectedListener。


原接口代码

interface R2OnClickFullListener {
    fun onFullScreen(fullscreen: Boolean)
}

原Fragment代码

class Rove2LiveVideoFragment : BaseFragment() {
    
    private lateinit var onFullScreenListener: R2OnClickFullListener

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_new_livevideo2, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        showBigView()
    }

    override fun onAttach(context: Context) {
        inject(this)
        super.onAttach(context)
        if (activity is OnFullScreenListener) { // 此处存在错误
            onFullScreenListener = activity as R2OnClickFullListener
        } else {
            throw ClassCastException(
                activity.toString()
                        + " must implement MyListFragment.OnItemSelectedListener" // 此处存在错误
            )
        }
    }

    private fun showBigView(){
        onFullScreenListener.onFullScreen(true)
    }
}

原Activity代码

class Rove2MainActivity : BaseActivity(), R2OnClickFullListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onFullScreen(fullscreen: Boolean) {
        // 实现逻辑
    }
}

错误信息

Process: com.rovedashcam.android, PID: 31327
java.lang.ClassCastException:
com.rovedashcam.newmodeule.rove2dashcam.main.home.view.Rove2MainActivity@82ae1e0
must implement MyListFragment.OnItemSelectedListener
at
com.rovedashcam.newmodeule.rove2dashcam.main.livevideo.view.Rove2LiveVideoFragment.onAttach(Rove2LiveVideoFragment.kt:80)


问题根源

  1. 类型判断错误:onAttach方法中的判断条件activity is OnFullScreenListener写错了,应该是activity is R2OnClickFullListener,导致明明Activity实现了正确接口,却进入else分支抛出异常。
  2. 异常提示字符串错误:抛出的异常信息里写的是要求实现MyListFragment.OnItemSelectedListener,这是遗留的旧代码,和当前业务的接口完全不符。

修正后的Fragment代码

class Rove2LiveVideoFragment : BaseFragment() {
    
    private lateinit var onFullScreenListener: R2OnClickFullListener

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_new_livevideo2, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        showBigView()
    }

    override fun onAttach(context: Context) {
        inject(this)
        super.onAttach(context)
        if (activity is R2OnClickFullListener) { // 修正判断条件
            onFullScreenListener = activity as R2OnClickFullListener
        } else {
            throw ClassCastException(
                activity.toString()
                        + " must implement R2OnClickFullListener" // 修正异常提示
            )
        }
    }

    private fun showBigView(){
        onFullScreenListener.onFullScreen(true)
    }
}

内容的提问来源于stack exchange,提问作者MARSH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 12:01:53