找不到符号 class 数据绑定实现
Cannot find symbol class Data binding impl
抱歉我不能post直接给post图片,因为它说我必须至少有10声望才能post它。
我在 android 工作室中创建了一个 xml 像这样
Xml
并像这样在视图模型中创建了 2 个变量
private val _loadingText = MutableLiveData<String>()
val loadingText: LiveData<String> = _loadingText
然后将这样的数据绑定实现到我的对话框视图中
val dialogView = layoutInflater.inflate(R.layout.dialog_custom_loading, dialog_root)
val binding = DialogCustomLoadingBinding.inflate(layoutInflater, dialogView as ViewGroup, false)
binding.viewModel = viewModel
loading = Dialog(this)
loading.setContentView(binding.root)
但是当我 运行 代码时,它显示了这样的错误
Error
我不知道如何解决..
请帮助我..
更新:
当我 运行 与 --stacktrace
我还是不知道这是什么错误..
The expression 'viewModelLoadingText.getValue()' cannot be inverted, so it cannot be used in a two-way binding
Details: There is no inverse for method getValue, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions
您正在使用双向数据绑定,这在此处不正确。
将android:text="@={viewModel.loadingText}"
更改为android:text="@{viewModel.loadingText}"
有关此问题的更多信息:当您还希望从 UI 更新数据时,使用双向数据绑定。一个案例可能是一个 EditText
,它将其文本发布到 MutableLiveData
并从中设置其文本。查看 official documentation 了解更多详情。
请尝试使用此代码实现自定义对话框的数据绑定。它对我来说很好
private fun showAddContactDialog() {
dialog= Dialog(this)
dialog.setCancelable(true)
dialog.setCanceledOnTouchOutside(true)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
Objects.requireNonNull(dialog.window)!!.setBackgroundDrawable(
ColorDrawable(Color.TRANSPARENT)
)
dialog.window!!.setGravity(Gravity.CENTER)
val dialogAddNotesBinding:DialogAddNotesBinding =DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.dialog_add_notes,null,false)
homeViewModel= ViewModelProviders.of(this).get(HomeViewModel::class.java)
dialogAddNotesBinding.homeModel=homeViewModel
homeViewModel.addNewContactListener=this
dialog.setContentView(dialogAddNotesBinding.root)
val window = dialog.window
window!!.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
dialog.show()
}
抱歉我不能post直接给post图片,因为它说我必须至少有10声望才能post它。
我在 android 工作室中创建了一个 xml 像这样 Xml
并像这样在视图模型中创建了 2 个变量
private val _loadingText = MutableLiveData<String>()
val loadingText: LiveData<String> = _loadingText
然后将这样的数据绑定实现到我的对话框视图中
val dialogView = layoutInflater.inflate(R.layout.dialog_custom_loading, dialog_root)
val binding = DialogCustomLoadingBinding.inflate(layoutInflater, dialogView as ViewGroup, false)
binding.viewModel = viewModel
loading = Dialog(this)
loading.setContentView(binding.root)
但是当我 运行 代码时,它显示了这样的错误
Error
我不知道如何解决.. 请帮助我..
更新: 当我 运行 与 --stacktrace 我还是不知道这是什么错误..
The expression 'viewModelLoadingText.getValue()' cannot be inverted, so it cannot be used in a two-way binding
Details: There is no inverse for method getValue, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions
您正在使用双向数据绑定,这在此处不正确。
将android:text="@={viewModel.loadingText}"
更改为android:text="@{viewModel.loadingText}"
有关此问题的更多信息:当您还希望从 UI 更新数据时,使用双向数据绑定。一个案例可能是一个 EditText
,它将其文本发布到 MutableLiveData
并从中设置其文本。查看 official documentation 了解更多详情。
请尝试使用此代码实现自定义对话框的数据绑定。它对我来说很好
private fun showAddContactDialog() {
dialog= Dialog(this)
dialog.setCancelable(true)
dialog.setCanceledOnTouchOutside(true)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
Objects.requireNonNull(dialog.window)!!.setBackgroundDrawable(
ColorDrawable(Color.TRANSPARENT)
)
dialog.window!!.setGravity(Gravity.CENTER)
val dialogAddNotesBinding:DialogAddNotesBinding =DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.dialog_add_notes,null,false)
homeViewModel= ViewModelProviders.of(this).get(HomeViewModel::class.java)
dialogAddNotesBinding.homeModel=homeViewModel
homeViewModel.addNewContactListener=this
dialog.setContentView(dialogAddNotesBinding.root)
val window = dialog.window
window!!.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
dialog.show()
}