findNavController().navigate() get invokeSuspend and Fragment$InstantiationException calling Fragment constructor caused an exception 错误

findNavController().navigate() get invokeSuspend and Fragment$InstantiationException calling Fragment constructor caused an exception error

logcat 错误:在 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()
                    }
                }
            }
        }

错误信息: androidx.fragment.app.Fragment$InstantiationException:无法实例化片段 .GoodInfoJourneysFragment:调用片段构造函数导致异常 在 androidx.fragment.app.Fragment. 实例化(Fragment.java:633) 在 androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 在 androidx.fragment.app.Fragment经理$2.instantiate(FragmentManager.java:448) 在 androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132) 在 androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162) 在 androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58) 在 androidx.navigation.NavController.navigate(NavController.java:1066) 在 androidx.navigation.NavController.navigate(NavController.java:944) 在 androidx.navigation.NavController.navigate(NavController.java:877) 在 androidx.navigation.NavController.navigate(NavController.java:863) 在 androidx.navigation.NavController.navigate(NavController.java:851) 在 $onViewCreated$1$1$1.invokeSuspend(CargoFragment.kt:97)

java.lang.NullPointerException 在 fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)

我改为-> private val binding 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!!
}

请检查第 17 行的 GoodInfoJourneysFragment 代码。异常堆栈跟踪中的行

java.lang.NullPointerException at fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)

表示您有一些您尝试使用的对象,但在使用之前尚未实例化。

添加

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

并尝试