gpt4 book ai didi

android - AgentDetailsActivityBindingImpl 类中所需的 DataBindingComponent 为空

转载 作者:行者123 更新时间:2023-12-03 13:27:09 27 4
gpt4 key购买 nike

在我的 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" />

Activity 中:
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 答案(绑定(bind)适配器方法必须是静态的):

在 kotlin 中,将您的绑定(bind)方法放在任何类之外(因此在您的情况下,在 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 .
里面还有很多其他的好技巧,我建议你研究一下关于 View 绑定(bind)的 yummies。

关于android - AgentDetailsActivityBindingImpl 类中所需的 DataBindingComponent 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com