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.

有什么 idea 吗?

推荐答案

Try this:

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

Kotlin相关问答推荐

将文本与文本字段的内容对齐

如何编写带有依赖项的自定义Kotlin串行化程序?

MyType.()在 Kotlin 中是什么意思?

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

Kotlin - 协程未按预期执行

如何缩短 MaterialTheme.colors.primary?

Kotlin 列表扩展功能

Mixin 在 Jackson 中添加 defaultImpl 不起作用

你怎么知道什么时候需要 yield()?

为什么我们在 kotlin 中需要 noinline?

Kotlin 方法重载

IntentService (kotlin) 的默认构造函数

Gradle 同步失败:不支持的方法:KotlinPlatformContainer.supports()

如何在 Kotlin 中判断数组类型(不是泛型类型)

Kotlin - 来自 KType 的 KClass<*>

Kotlin 接口属性:只需要公共 getter 而没有公共 setter

使用导航组件在不同的图形之间导航

如何启用spring security kotlin DSL?

Kotlin flatMap - map

Kotlin中默认导入哪些包/函数?