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

时间:2019-03-15 01:14:17

标签: android android-fragments android-navigation android-jetpack android-architecture-navigation

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

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

该应用程序以前可以正常运行,但是由于某种原因它不再起作用,并且会出现错误:

  

androidx.fragment.app.Fragment $ InstantiationException:无法执行   实例化片段   com.muchammadagunglaksana.km.Fragments.Authentication.LoginUsingEmailFragment:   调用Fragment构造函数导致异常

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$1.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$1.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"/>

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

1 个答案:

答案 0 :(得分:1)

根据错误消息:

  

原因:kotlin.UninitializedPropertyAccessException:lateinit属性mContext尚未初始化

     

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

您正在访问构造函数中的mContext,但是您的lateinit mContext变量尚未初始化(不会初始化,因为在构造过程中未附加Fragment )。