This is my interface
interface R2OnClickFullListener {
fun onFullScreen(fullscreen: Boolean)
}
This is my 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)
}
}
This is my activity
class Rove2MainActivity : BaseActivity(),R2OnClickFullListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onFullScreen(fullscreen: Boolean) {
}
}
But i am getting class cast exception basically i am trying to pass data from fragment to activity so on attach fragment i am registering callback please help what i am doing wrong why the class cast exception is coming
below is error
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)
CodePudding user response:
it should be
if (activity is Rove2MainActivity ) {
onFullScreenListener = activity as R2OnClickFullListener
} else {
throw ClassCastException(
activity.toString()
" must implement MyListFragment.OnItemSelectedListener"
)
}