当我构建应用程序(构建但尚未运行)时,
我遇到构建错误:
但是我确实在CreateTaskForm
kotlin类中添加了addTask方法,因此不确定出了什么问题。
下面是 fragment 类CreateTaskForm,
class CreateTaskForm:Fragment (){
var addTask:(String,Boolean)-> Unit={s,b-> }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
var binding:FragmentCreateTaskFormBinding =
DataBindingUtil.inflate(inflater,R.layout.fragment_create_task_form, container,false)
binding.me=this
return binding.root
}
以下是 fragment 布局fragment_create_task_form
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="me" type="com.edenhan.simplytask.CreateTaskForm">
</variable>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnCreateTask"
android:background="@android:drawable/ic_menu_add"
android:onClick="@{(v)->me.addTask(`ds`,true)}"
.../>
最佳答案
数据绑定(bind)无法在CreateTaskForm中找到方法,可能无法将您的类函数字段识别为类方法。
我看到两个解决方案:
首先,将字段更改为在CreateTaskForm中起作用:
fun addTask(value: String, flag: Boolean) {
}
其次,直接从xml中的功能字段调用:
<Button
android:id="@+id/btnCreateTask"
android:background="@android:drawable/ic_menu_add"
android:onClick="@{(v)->me.addTask.invoke(`ds`,true)}"
.../>
关于android - Kotlin Android-数据绑定(bind)在我的 fragment 中失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49962178/