android-fragments com.chargeatfriends.android.ui.reserve.ReserveFragment:调用Fragment构造函数是否导致异常?

xtfmy6hx  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(136)

我正在开发新项目,运行项目时遇到以下异常

E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.chargeatfriends.android, PID: 31595
        androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.chargeatfriends.android.ui.reserve.ReserveFragment: calling Fragment constructor caused an exception
           com.chargeatfriends.android.ui.map.MapFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
            at android.view.View.performClick(View.java:7870)
            at android.widget.TextView.performClick(TextView.java:14970)
            at android.view.View.performClickInternal(View.java:7839)
            at android.view.View.access$3600(View.java:886)
            at android.view.View$PerformClick.run(View.java:29363)
            at android.os.Handler.handleCallback(Handler.java:883)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:237)
            at android.app.ActivityThread.main(ActivityThread.java:7948)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
            at androidx.fragment.app.Fragment.instantiate(Fragment.java:615)
            at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
            at androidx.fragment.app.FragmentManager$2.instantiate(FragmentManager.java:448) 
            at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.kt:190) 
            at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.kt:162) 
            at androidx.navigation.NavController.navigateInternal(NavController.kt:260) 
            at androidx.navigation.NavController.navigate(NavController.kt:1719) 
            at androidx.navigation.NavController.navigate(NavController.kt:1545) 
            at androidx.navigation.NavController.navigate(NavController.kt:1472) 
            at androidx.navigation.NavController.navigate(NavController.kt:1454) 
            at androidx.navigation.NavController.navigate(NavController.kt:1437) 
            at com.chargeatfriends.android.ui.map.MapFragment.clickListener$lambda-0(MapFragment.kt:176) 
            at com.chargeatfriends.android.ui.map.MapFragment.$r8$lambda$f_HcQmFOF4yHY3eWCzm1TTIl2BA(Unknown Source:0) 
            at com.chargeatfriends.android.ui.map.MapFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:2) 
            at android.view.View.performClick(View.java:7870) 
            at android.widget.TextView.performClick(TextView.java:14970) 
           
         Caused by: kotlin.UninitializedPropertyAccessException: lateinit property sharedPref has not been initialized
            at com.chargeatfriends.android.ui.reserve.ReserveFragment.getSharedPref(ReserveFragment.kt:61)
            at com.chargeatfriends.android.ui.reserve.ReserveFragment.<init>(ReserveFragment.kt:97)
            at java.lang.reflect.Constructor.newInstance0(Native Method) 
            at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
            at androidx.fragment.app.Fragment.instantiate(Fragment.java:615) 
            at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
            at androidx.fragment.app.FragmentManager$2.instantiate(FragmentManager.java:448) 
            at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.kt:190) 
            at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.kt:162) 
            at androidx.navigation.NavController.navigateInternal(NavController.kt:260) 
            at androidx.navigation.NavController.navigate(NavController.kt:1719) 
            at androidx.navigation.NavController.navigate(NavController.kt:1545) 
            at androidx.navigation.NavController.navigate(NavController.kt:1472) 
            at androidx.navigation.NavController.navigate(NavController.kt:1454) 
            at androidx.navigation.NavController.navigate(NavController.kt:1437) 
            at com.chargeatfriends.android.ui.map.MapFragment.clickListener$lambda-0(MapFragment.kt:176) 
            at com.chargeatfriends.android.ui.map.MapFragment.$r8$lambda$f_HcQmFOF4yHY3eWCzm1TTIl2BA(Unknown Source:0) 
            at com.chargeatfriends.android.ui.map.MapFragment$

(未知来源:2)
在我的ReserveFragment.kt代码下面巨大的这就是为什么共享为gist https://gist.github.com/kyodgorbek/b78a0fa14f949baf9209b8ff5914aa02
在我MapFragment.kt下面,当我单击上面的按钮时,出现异常
查看.OnClickListener {查看-〉

when (view.getId()) {
        R.id.gotoBookingButton -> {
            sharedPref.CurrentChargePoint = CurrentChargePoint

            if (sharedPref.token.length > 0) {
                Log.d("@@@@@@@", "${sharedPref.token}: ")
                sharedPref.isAdhocBooking = false
                findNavController().navigate(R.id.navigation_reserve) // in this line

            } else {
                findNavController().navigate(R.id.navigation_settings_login)
            }
        }
        R.id.mapView -> {
            StationInfo.isVisible = false
        }
        R.id.qrButton -> {
            findNavController().navigate(R.id.activity_qr)
        }
    }
}

在我使用SharedPreferences的SharedPref.kt类下面
在我的服务器模块和共享首选项下。kt类https://gist.github.com/kyodgorbek/84f77dfd49a21066339053d5c6118883
我所尝试的我尝试了calling Fragment constructor caused an exception, kotlin?
所有stackoverflow回答模拟问题和无效缓存重新启动和调试它以及不成功任何建议非常感谢

ki0zmccv

ki0zmccv1#

问题出在这个变量上

@Inject
lateinit var sharedPref:SharedPref

您在此行中使用它

var EndTime: LocalTime = LocalTime.now().plusSeconds(3600 + sharedPref.defaultChargeTime)

如果您提供SharedPref类,请检查您依赖模块

相关问题