在使用kotlin的时候,我们都知道通过在配置文件build.gradle中添加:
apply plugin:'kotlin-android-extensions
就可以通过ID获取控件的实例了。如图:
但是最近添加了最新版的Glide图片加载库之后,发现在Fragment中通过ID就找不到控件的实例了,在Acitity中可以。
如下:
implementation 'com.github.bumptech.glide:glide:4.11.0'
运行的时候报错,错误信息如下:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val Activity.recyclerView: RecyclerView! defined in kotlinx.android.synthetic.main.fragment_refresh_layout
public val Dialog.recyclerView: RecyclerView! defined in kotlinx.android.synthetic.main.fragment_refresh_layout
public val android.app.Fragment.recyclerView: RecyclerView! defined in kotlinx.android.synthetic.main.fragment_refresh_layout
public val androidx.fragment.app.Fragment.recyclerView: RecyclerView! defined in kotlinx.android.synthetic.main.fragment_refresh_layout
意思是未解决的参考。由于接收方类型不匹配,下列候选项均不适用:
找了好久都没有解决,但又不想通过findViewById()来获取实例。后来我降低了Gidle版本使用了如下:
implementation 'com.github.bumptech.glide:glide:4.8.0'
发现降低了版本之后,在Fragment就可以通过ID获取实例了。我想是应该最新版Glide库和Kotlin某些地方有冲突吧,具体原因没有找到,找到问题的可以告知一下。