KotterKnife - 不能对某些类使用bindView(R.id.example_id)

时间:2015-02-16 19:51:36

标签: android kotlin butterknife

我在尝试使用KotterKnife时遇到以下错误 - 但仅限于某些类。这是怎么回事?

e: /Users/user/dev/git/to14/android/src/main/kotlin/com.example/adapters/ChapterListAdapter.kt: (59, 34): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public fun <T : android.view.View> android.app.Dialog.bindView(id: kotlin.Int): kotlin.properties.ReadOnlyProperty<kotlin.Any, android.widget.TextView> defined in butterknife
public fun <T : android.view.View> android.support.v4.app.Fragment.bindView(id: kotlin.Int): kotlin.properties.ReadOnlyProperty<kotlin.Any, android.widget.TextView> defined in butterknife
public fun <T : android.view.View> android.app.Fragment.bindView(id: kotlin.Int): kotlin.properties.ReadOnlyProperty<kotlin.Any, android.widget.TextView> defined in butterknife
public fun <T : android.view.View> android.view.ViewGroup.bindView(id: kotlin.Int): kotlin.properties.ReadOnlyProperty<kotlin.Any, android.widget.TextView> defined in butterknife
public fun <T : android.view.View> android.support.v7.widget.RecyclerView.ViewHolder.bindView(id: kotlin.Int): kotlin.properties.ReadOnlyProperty<kotlin.Any, android.widget.TextView> defined in butterknife
public fun <T : android.view.View> android.app.Activity.bindView(id: kotlin.Int): kotlin.properties.ReadOnlyProperty<kotlin.Any, android.widget.TextView> defined in butterknife

1 个答案:

答案 0 :(得分:5)

Kotterknife的bindView代表仅适用于ActivityDialogViewGroupFragment,支持库Fragment的子类,以及回收者视图的ViewHolder。我猜您正在尝试将视图绑定为ListAdapter的属性,但要使其工作,您必须拥有可以调用findViewById的对象。

对于非R​​ecyclerView ViewHolder,您可以尝试

class ViewHolder(view: View) { 
    val textView: TextView = view.findViewById(R.id.name) as TextView 
}