Type mismatch: Required: Context, Found: Intent

Zorgan :

I've created a custom Preference ClickPreference:

class ClickPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), View.OnClickListener {

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        val box = holder?.itemView
        box?.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        action(title)
    }

    fun action(title: CharSequence){
        when (title){
            "email" -> {
                ...
            }
            "Logout" -> {
                LoginManager.getInstance().logOut()
                val intent = Intent(context, MainActivity::class.java) // context is from getContext()
                startActivity(intent)
            }
        }
    }


}

However I'm getting this error on startActivity():

enter image description here

I don't understand why it's wrong as it's a valid Intent constructor.

Any idea?

Yevhenii :

Try this:

val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=121528&siteId=1