DialogFragment:"lateinit property binding has not been initialized"在onCreateDialog()中,而绑定是在onViewCerated()中发起的
DialogFragment: "lateinit property binding has not been initialized" in onCreateDialog(), whereas binding is initiated in onViewCerated()
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
at space.rodionov.financialsobriety.ui.transaction.edittransaction.ChooseCategoryDialogFragment.onCreateDialog(ChooseCategoryDialogFragment.kt:57)
当我导航到其中包含 RecyclerView 的 DialogFragment 时出现此错误。我需要 Dialog 来包含 recyclerview 和来自 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 之前执行,此时您的视图未创建。
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
}
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
at space.rodionov.financialsobriety.ui.transaction.edittransaction.ChooseCategoryDialogFragment.onCreateDialog(ChooseCategoryDialogFragment.kt:57)
当我导航到其中包含 RecyclerView 的 DialogFragment 时出现此错误。我需要 Dialog 来包含 recyclerview 和来自 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 之前执行,此时您的视图未创建。
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
}