android-fragments findNavController().navigate()获取invokeSuspend和片段$InstantiationException调用片段构造函数导致异常错误

rkkpypqq  于 2022-11-13  发布在  Android
关注(0)|答案(3)|浏览(165)

logcat error: invoke suspend at findNavController().navigate(navDirections)

viewLifecycleOwner.lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.goodInfoStateFlow.collectLatest { goodInfoState ->
                    when (goodInfoState) {
                        is GoodInfoState.Success -> {

                            val goodInfo = goodInfoState.goodInfo

                            Log.d(TAG, "goodInfo: $goodInfo")

                                val navDirections =
                            CargoFragmentDirections.actionCargoFragmentToGoodInfoJourneysFragment(
                                goodInfo
                            )
                        findNavController().navigate(navDirections)

                        }
                        is GoodInfoState.Error -> {
                            AlertDialog.Builder(requireContext())
                                .setMessage(goodInfoState.message)
                                .setNegativeButton(
                                    "Cancel"
                                ) { dialogInterface, _ ->
                                    dialogInterface.dismiss()
                                }
                        }
//                        GoodInfoState.Loading -> TODO()
                    }
                }
            }
        }

Error Message:
androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment .GoodInfoJourneysFragment: calling Fragment constructor caused an exception at androidx.fragment.app.Fragment.instantiate(Fragment.java:633) at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) at androidx.fragment.app.FragmentManager$2.instantiate(FragmentManager.java:448) at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132) at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162) at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58) at androidx.navigation.NavController.navigate(NavController.java:1066) at androidx.navigation.NavController.navigate(NavController.java:944) at androidx.navigation.NavController.navigate(NavController.java:877) at androidx.navigation.NavController.navigate(NavController.java:863) at androidx.navigation.NavController.navigate(NavController.java:851) at $onViewCreated$1$1$1.invokeSuspend(CargoFragment.kt:97) java.lang.NullPointerException at fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)

jei2mxaa

jei2mxaa1#

在我更改为-〉private瓦尔绑定get()=之后
并且解决了这个问题。
产地:

class CargoFragment : Fragment() {

    private var _binding: FragmentCargoBinding? = null
    private val binding = _binding!!
}

修改后:

class CargoFragment : Fragment() {

    private var _binding: FragmentCargoBinding? = null
    private val binding get() = _binding!!
}
ego6inou

ego6inou2#

请检查第17行的GoodInfoJourneysFragment代码。异常堆栈跟踪中的这一行
java.lang.NullPointerException at fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)
表示您尝试使用某个对象,但在使用之前尚未示例化。

11dmarpk

11dmarpk3#

新增

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

并尝试了

相关问题