Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type mismatch: Required: Context, Found: Intent

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?

like image 442
Zorgan Avatar asked Jun 15 '19 09:06

Zorgan


1 Answers

Try this:

val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
like image 80
Yevhenii Avatar answered Nov 15 '22 19:11

Yevhenii