Android BottomSheetDialogFragment 中正确的引用liveData

ava.lang.IllegalStateException: Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()
                                                                                                        at androidx.fragment.app.Fragment.getViewLifecycleOwner(Fragment.java:361)
                                                                                                        at co.haive.lib.common.v2.ui.dialog.bottom.sheet.PlayListBottomSheetDialog.initView(PlayListBottomSheetDialog.kt:118)
                                                                                                        at co.haive.lib.common.v2.ui.dialog.bottom.sheet.PlayListBottomSheetDialog.onCreateDialog(PlayListBottomSheetDialog.kt:54)
                                                                                                        at androidx.fragment.app.DialogFragment.prepareDialog(DialogFragment.java:644)
                                                                                                        at androidx.fragment.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:558)
                                                                                                        at co.haive.lib.common.v2.ui.dialog.bottom.sheet.Hilt_PlayListBottomSheetDialog.onGetLayoutInflater(Hilt_PlayListBottomSheetDialog.java:74)

这里因为用到了 

BottomSheetDialogFragment 则不应当使用 LifecycleOwner 

因为当LifecycleOwner 无论放在那个生命周期中都会报错。。直接替换为

requireActivity() 则可以直接解决这个问题 

可以把这些监听逻辑放到 

dialog.setContentView(view) 上面。这样能保证视图出来之前数据被正确设置。

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/129726178
今日推荐