简体   繁体   English

安卓数据绑定。 按钮 onClick 不起作用

[英]Android Data Binding. Button onClick not working

I am stuck here, help.我卡在这里了,求助。

I've got the following code:我有以下代码:

ProfileFragment:简介片段:

    @AndroidEntryPoint
    class ProfileFragment : Fragment() {
        private val profileViewModel: ProfileViewModel by viewModels()
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val binding = FragmentProfileBinding.inflate(inflater, container, false)
            binding.viewModel = profileViewModel
            binding.lifecycleOwner = viewLifecycleOwner
    
            return binding.root
        }
    }

I am not sure if this issue still exist or not but if you are using data binding and hilt for dependency injection please add below lines in your fragment onViewCreated我不确定这个问题是否仍然存在,但如果您使用数据绑定和 hilt 进行依赖注入,请在您的片段 onViewCreated 中添加以下几行

binding.lifecycleOwner = this
binding.viewModel = profileViewModel

What fixed this problem for me was adding the fragment reference to itself in onCreateView<\/em> :为我解决这个问题的方法是在onCreateView<\/em>中添加对自身的片段引用:

binding.<fragment_name> = this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM