Home > Software design >  Issues related to moving to MainActivity by pressing a button
Issues related to moving to MainActivity by pressing a button

Time:04-21

If the user presses the positive button, they want to move it to MainActivity

fun setPositiveButtonClick(callback: (() -> Unit)?, view: View? = null) {
        val intent = Intent(this@ErrorDialog, MainActivity::class.java)
        startActivity(intent)
        var view = view
        if (view == null) view = this.view
        this.positiveCallback = callback

        view?.let { v ->
            v.yes.setOnClickListener {
                callback?.let { c ->
                    c()
                }
                dismiss()
            }

        }
    }

Here is the error

enter image description here

CodePudding user response:

I'm assuming this is in some sort of Dialog class, so I think you simply need to get the context of it by just replacing

val intent = Intent(this@ErrorDialog, MainActivity::class.java)

to

val intent = Intent(this.context, MainActivity::class.java)
  • Related