types推断失败。 请尝试指定types参数显式:Kotlin

class SetContentView( @LayoutRes private val layoutRes: Int) { private var value : T? = null operator fun getValue(thisRef: Activity, property: KProperty): T { value = value ?: DataBindingUtil.setContentView(thisRef, layoutRes) return value } } val binding: ActivityDogBinding by contentView(R.layout.activity_dog) 

DogActivity.kt

  fun  contentView(@LayoutRes layoutRes: Int): SetContentView { return SetContentView(layoutRes) } 

当我尝试打电话给活动

 val binding: ActivityLoginBinding by contentView(layoutRes = R.layout.activity_login) 

它的投掷错误为

types推断失败。 请尝试明确指定types参数:Kotlin

在Android Studio中。 我在IntelliJ中尝试类似的工作正常。

您需要为您的getValue方法使用Rtypes参数:

 operator fun getValue(thisRef: R, property: KProperty<*>): T { ... }