作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 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 答案(绑定(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);
}
...
}
关于android - AgentDetailsActivityBindingImpl 类中所需的 DataBindingComponent 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049179/
在我的 agent_details_activity.xml 中: Activity 中: class AgentDetailsActivity : AppCompatActivity() {
我是一名优秀的程序员,十分优秀!