使用导航控制器 android 从一个方向导航到另一个方向时无法实例化片段?

Unable to instantiate fragment when navigating from one direction to another using navigation controller android?

使用以下代码从一个片段导航到另一个片段时,我通常没有问题:

           val login = AuthenticationFragmentDirections.actionToLogin()
            Navigation.findNavController(fragmentView).navigate(login)

该应用程序以前运行良好,但不知何故它不再运行并出现错误:

androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.muchammadagunglaksana.km.Fragments.Authentication.LoginUsingEmailFragment: calling Fragment constructor caused an exception

androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.LoginUsingEmailFragment: calling Fragment constructor caused an exception
        at androidx.fragment.app.Fragment.instantiate(Fragment.java:503)
        at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:184)
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:212)
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58)
        at androidx.navigation.NavController.navigate(NavController.java:822)
        at androidx.navigation.NavController.navigate(NavController.java:807)
        at androidx.navigation.NavController.navigate(NavController.java:744)
        at androidx.navigation.NavController.navigate(NavController.java:730)
        at androidx.navigation.NavController.navigate(NavController.java:862)
        at com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.AuthenticationFragment$setUpListeners.onClick(AuthenticationFragment.kt:55)
        at android.view.View.performClick(View.java:5246)
        at android.widget.TextView.performClick(TextView.java:10565)
        at android.view.View$PerformClick.run(View.java:21200)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6946)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at androidx.fragment.app.Fragment.instantiate(Fragment.java:485)
        at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:184) 
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:212) 
        at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58) 
        at androidx.navigation.NavController.navigate(NavController.java:822) 
        at androidx.navigation.NavController.navigate(NavController.java:807) 
        at androidx.navigation.NavController.navigate(NavController.java:744) 
        at androidx.navigation.NavController.navigate(NavController.java:730) 
        at androidx.navigation.NavController.navigate(NavController.java:862) 
        at com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.AuthenticationFragment$setUpListeners.onClick(AuthenticationFragment.kt:55) 
        at android.view.View.performClick(View.java:5246) 
        at android.widget.TextView.performClick(TextView.java:10565) 
        at android.view.View$PerformClick.run(View.java:21200) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:145) 
        at android.app.ActivityThread.main(ActivityThread.java:6946) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property mContext has not been initialized
        at com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.LoginUsingEmailFragment.<init>(LoginUsingEmailFragment.kt:47)

从身份验证我希望用户可以选择使用代码登录或注册目的地

toLoginButton.setOnClickListener {

            val login = AuthenticationFragmentDirections.actionToLogin()
            Navigation.findNavController(fragmentView).navigate(login)

        }

        toRegisterButton.setOnClickListener {

            val register = AuthenticationFragmentDirections.actionToRegister()
            Navigation.findNavController(fragmentView).navigate(register)

        }

但我不知道为什么会出现该错误:

这是我的导航图 xml:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_graph"
            app:startDestination="@id/destination_home">

    <fragment android:id="@+id/destination_authentication"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.AuthenticationFragment"
              tools:layout="@layout/fragment_authentication">
        <action android:id="@+id/action_toLogin"
                app:destination="@id/destination_loginUsingEmail"/>
        <action android:id="@+id/action_toRegister"
                app:destination="@id/destination_registerUsingEmail"/>
    </fragment>

    <fragment android:id="@+id/destination_registerUsingEmail"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.RegisterUsingEmailFragment"
              tools:layout="@layout/fragment_register_using_email"/>

    <fragment android:id="@+id/destination_welcome"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.WelcomeAndOnBoarding.WelcomeFragment"
              tools:layout="@layout/fragment_welcome">
        <action android:id="@+id/action_toAuthentication"
                app:destination="@id/destination_authentication"/>
    </fragment>

    <fragment android:id="@+id/destination_change_password"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.ChangePasswordFragment"
              tools:layout="@layout/fragment_change_password"/>

    <fragment android:id="@+id/destination_loginUsingEmail"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.LoginUsingEmailFragment"
              tools:layout="@layout/fragment_login_using_email">
        <action android:id="@+id/action_toChangePassword"
                app:destination="@id/destination_change_password"/>
    </fragment>

    <fragment android:id="@+id/destination_home" android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Home.HomeFragment"
              tools:layout="@layout/fragment_home"/>
    <action android:id="@+id/action_global_homeFragment" app:destination="@id/destination_home"/>

    <fragment android:id="@+id/destination_create_event"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.CreateEvent.CreateEventFragment"
              tools:layout="@layout/fragment_create_event"/>

    <fragment android:id="@+id/destination_user_control"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.UserControl.UserControlFragment"
              tools:layout="@layout/fragment_profile"/>

    <fragment android:id="@+id/destination_search" android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Search.SearchFragment"
              tools:layout="@layout/fragment_search"/>

    <fragment android:id="@+id/destination_choose_user_location"
              android:name="com.muchammadagunglaksana.kumpul_muslim.Fragments.Reusable.ChooseUserLocationFragment"
              tools:layout="@layout/fragment_choose_user_location"/>
    <action android:id="@+id/action_global_chooseUserLocationFragment" app:destination="@id/destination_choose_user_location"/><action android:id="@+id/action_global_destination_welcome" app:destination="@id/destination_welcome"/>

真的需要你的帮助,因为我是导航组件的新手

根据错误信息:

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property mContext has not been initialized

at com.muchammadagunglaksana.kumpul_muslim.Fragments.Authentication.LoginUsingEmailFragment.(LoginUsingEmailFragment.kt:47)

您正在构造函数中访问 mContext,但是您的 lateinit mContext 变量尚未初始化(不会初始化,因为未附加片段施工期间)。