【问题标题】:DialogFragment: "lateinit property binding has not been initialized" in onCreateDialog(), whereas binding is initiated in onViewCerated()DialogFragment:onCreateDialog() 中的“lateinit 属性绑定尚未初始化”,而绑定是在 onViewCerated() 中启动的
【发布时间】:2021-06-23 01:34:36
【问题描述】:
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
        at space.rodionov.financialsobriety.ui.transaction.edittransaction.ChooseCategoryDialogFragment.onCreateDialog(ChooseCategoryDialogFragment.kt:57)

当我导航到包含 RecyclerView 的 DialogFragment 时,会出现此错误。我需要 Dialog 来包含可从 Room 中选择自定义项目的回收站视图。

如果我在 onViewCreated 中初始化了绑定,为什么会遇到这个错误,解决方法是什么? 感谢任何帮助

我的 DialogFragment:

@AndroidEntryPoint
class ChooseCategoryDialogFragment : DialogFragment(), ChooseCategoryAdapter.OnItemClickListener {

    private val viewModel: ChooseCategoryViewModel by viewModels()

    lateinit var binding: FragmentChooseCategoryBinding

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding = FragmentChooseCategoryBinding.bind(view)
        val chooseCatAdapter = ChooseCategoryAdapter(this)

        binding.apply {
            recyclerView.apply {
                adapter = chooseCatAdapter
                layoutManager = LinearLayoutManager(requireContext())
                setHasFixedSize(true)

                viewLifecycleOwner.lifecycleScope.launchWhenStarted {
                    viewModel.categories.collect {
                        val spends = it ?: return@collect

                        chooseCatAdapter.submitList(spends)
                        tvNoCategories.isVisible = spends.isEmpty()
                        recyclerView.isVisible = spends.isNotEmpty()
                    }
                }
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

            return AlertDialog.Builder(requireContext())
                .setTitle(requireContext().resources.getString(R.string.choose_category))
                .setView(binding.root) // here is the 57th line that error message alludes to.
                .setNegativeButton(
                    requireContext().resources.getString(R.string.cancel_action),
                    null
                )
                .create()
    }   

    override fun onItemClick(category: Category) {
        // some action with object
    }
}

【问题讨论】:

  • 据我所知,onCreateDialog 被称为 之前 onViewCreated。但是,您可以通过调试/日志轻松验证这一点。
  • 感谢您的帮助,我将初始化重新定位到 onCreateDialog() 并且它工作正常。

标签: android kotlin android-dialogfragment android-viewbinding fragment-oncreateview


【解决方案1】:

试试下面的代码,它会对你有所帮助,onCreateDialog() 在 onViewCreated 之前执行,那时你的视图没有创建。

 lateinit var binding: FragmentChooseCategoryBinding

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    _binding = FragmentChooseCategoryBinding.inflate(LayoutInflater.from(context))
    return AlertDialog.Builder(requireActivity())
        .setView(binding.root)
        .create()
}

override fun onDestroyView() {
    super.onDestroyView()
    binding = null
} 

【讨论】:

    猜你喜欢
    • 2021-08-20
    • 2021-10-13
    • 2017-03-03
    • 1970-01-01
    • 2020-12-31
    • 2021-06-11
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多