android - AgentDetailsActivityBindingImpl 类中所需的 DataBindingComponent 为空
问题描述
在我的 agent_details_activity.xml 中:
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="0dp"
android:layout_height="400dp"
app:agentImageUrl="@{item.url}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />
活动中:
class AgentDetailsActivity : AppCompatActivity() {
private lateinit var binding: AgentDetailsActivityBinding
companion object {
val AGENT_DETAILS = AgentDetailsActivity::class.java.name + "_AGENT_DETAILS"
}
private val TAG = AgentDetailsActivity::class.java.name
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = AgentDetailsActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.setHandler(this);
}
@BindingAdapter("agentImageUrl")
fun loadImage(view: ImageView, imageUrl: String) {
Glide.with(view.context)
.load(imageUrl)
.apply(RequestOptions().error(R.drawable.no_image))
.into(view)
}
但在运行时我得到错误:
Shutting down VM
FATAL EXCEPTION: main
Process: com.android.testproject.android.kotlin.debug, PID: 24924
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.testproject.android.kotlin.debug/com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.AgentDetailsActivity}: java.lang.IllegalStateException: Required DataBindingComponent is null in class AgentDetailsActivityBindingImpl. A BindingAdapter in com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.AgentDetailsActivity is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Required DataBindingComponent is null in class AgentDetailsActivityBindingImpl. A BindingAdapter in com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.AgentDetailsActivity is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static.
at androidx.databinding.ViewDataBinding.ensureBindingComponentIsNotNull(ViewDataBinding.java:666)
at com.android.testproject.android.kotlin.databinding.AgentDetailsActivityBindingImpl.<init>(AgentDetailsActivityBindingImpl.java:40)
at com.android.testproject.android.kotlin.databinding.AgentDetailsActivityBindingImpl.<init>(AgentDetailsActivityBindingImpl.java:32)
at com.android.testproject.android.kotlin.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:52)
at androidx.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)
at androidx.databinding.DataBindingUtil.bind(DataBindingUtil.java:199)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:130)
at androidx.databinding.ViewDataBinding.inflateInternal(ViewDataBinding.java:1366)
at com.android.testproject.android.kotlin.databinding.AgentDetailsActivityBinding.inflate(AgentDetailsActivityBinding.java:95)
at com.android.testproject.android.kotlin.databinding.AgentDetailsActivityBinding.inflate(AgentDetailsActivityBinding.java:81)
at com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.AgentDetailsActivity.onCreate(AgentDetailsActivity.kt:37)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
解决方案
详细说明@mahdi shahbazi 答案(绑定适配器方法必须是静态的):
在 kotlin 中,将您的绑定方法放在任何类之外(因此在您的情况下,在 Activity 类之外)。这会成功的。在你的情况下,它看起来像:
@BindingAdapter("agentImageUrl")
fun loadImage(view: ImageView, imageUrl: String) {
Glide.with(view.context)
.load(imageUrl)
.apply(RequestOptions().error(R.drawable.no_image))
.into(view)
}
class AgentDetailsActivity : AppCompatActivity() {
private lateinit var binding: AgentDetailsActivityBinding
companion object {
val AGENT_DETAILS = AgentDetailsActivity::class.java.name + "_AGENT_DETAILS"
}
private val TAG = AgentDetailsActivity::class.java.name
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = AgentDetailsActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.setHandler(this);
}
...
}
由于这篇文章,我能够想出这个答案:https ://proandroiddev.com/custom-attributes-using-bindingadapters-in-kotlin-971ef8fcc259 。里面还有很多其他的好技巧,我建议你研究一下关于视图绑定的 yummies。
推荐阅读
- java - 使用公共 wss (Java) 获取 websocket 数据
- python - 更新 Seaborn 后 Python 无法启动
- java - java如何用char数组实现我的对象类到一个主类
- postgresql - pgbouncer 抛出 Pooler Error: Auth Failed when using datagrip but not when using pgadmin
- c# - 在 wpf 应用程序中打开控制台
- java - 在 graal 中将 spnego 和 kerberos 与 gss 一起使用
- haskell - 使用列表理解的非负数的数字总和
- python - 用于使用 pandas、os、glob 删除列的 Python 代码更改整个表的值
- python - matplotlib with barh in python
- java - 问号而不是java中的String