类型不匹配:必需:上下文,找到:目的

时间:2019-06-15 09:23:45

标签: java android kotlin

我已经创建了自定义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)
            }
        }
    }


}

但是我在startActivity()上遇到此错误:

enter image description here

我不明白为什么这是错误的,因为它是有效的Intent constructor

有什么主意吗?

1 个答案:

答案 0 :(得分:3)

尝试一下:

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