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

Zak*_*rdi 3 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
Run Code Online (Sandbox Code Playgroud)

Pio*_*iel 5

Kotterknife的bindView代表将只对sublclasses Activity,Dialog,ViewGroup,Fragment,支持库Fragment,以及回收视图的ViewHolder.我猜你试图将视图绑定为属性ListAdapter,但为了让它工作,你必须有你可以调用的对象findViewById.

对于非RecyclerView,ViewHolder您可以尝试

class ViewHolder(view: View) { 
    val textView: TextView = view.findViewById(R.id.name) as TextView 
}
Run Code Online (Sandbox Code Playgroud)