java.lang.ClassCastException:com.example.readdoang.ui.login_regist_activity.LoginRegistActivity无法转换为com

问题描述 投票:0回答:1

Hello im new in android

在MVP结构上实现导航图时出现错误,我不知道如何解决,我的问题是如何从视图片段类中调用presenter。

这是我的片段类

class RegisterFragment : Fragment(), RegisterView.View {

    private lateinit var presenter : RegisterView.Presenter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val root = inflater.inflate(R.layout.fragment_register, container, false)
        buttonAction(root)
        return root
    }

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

    private fun buttonAction(root: View) {
        val name: String = root.txt_email.text.toString()
        val email: String = root.txt_name.text.toString()
        val password: String = root.txt_password.text.toString()

        presenter = (activity as RegisterView.Presenter?)!!

        root.btnSignUp.setOnClickListener {
            presenter.sendData(name, email, password)
        }

    }

    override fun onDataRegisterComplete(response: User) {
        Log.d("Reponse : ", "$response")
    }

    override fun onDataRegisterFailure(throwable: Throwable) {
        Log.d("Response : ", "$throwable")
    }
}

这是我的视图界面

interface RegisterView {
    interface View {
        fun onDataRegisterComplete(response: User)
        fun onDataRegisterFailure(throwable: Throwable)
    }

    interface Presenter {
        fun sendData(email: String, name: String, password: String)
    }
}

这是我的主持人

class RegisterPresenter(context : Context?) : RegisterView.Presenter {

    private val regist = context as RegisterView.View

    override fun sendData(name: String, email: String, password: String) {

    }

}

而且我在主持人中什么也没做

这是我的日志错误消息

java.lang.ClassCastException: com.example.readdoang.ui.login_regist_activity.LoginRegistActivity cannot be cast to com.example.readdoang.ui.register_activity.RegisterView$Presenter
        at com.example.readdoang.ui.register_activity.RegisterFragment.buttonAction(RegisterFragment.kt:37)
        at com.example.readdoang.ui.register_activity.RegisterFragment.onCreateView(RegisterFragment.kt:24)

谢谢

您好,我是android的新手,在MVP结构上实现导航图时出现错误,我不知道如何解决,我的问题是如何从视图片段类中调用presenter,这是...

android kotlin mvp android-jetpack android-jetpack-navigation
1个回答
0
投票

在您的类RegisterFragment中,重写onCreateView(),在这种方法中,您可以像这样将视图(片段)传递给演示者:

© www.soinside.com 2019 - 2024. All rights reserved.