我在谷歌地图的MapView组件上遇到了一个奇怪的问题,导致运行时异常UninitializedPropertyAccessException在第二次配置更改(设备旋转(后,除MapView的方法被覆盖的视图外,其他每个视图都会引发异常因此,在处理com.google.android.gms.maps.MapView
生命周期的MyMapViewFragment
中,不会引发此错误,并且配置更改工作正常。
每隔一个视图都会出现问题,当我旋转设备时,调用MyMapViewFragment.onDestroy()
,这会破坏com.google.android.gms.maps.MapView
:
override fun onDestroy() {
super.onDestroy()
googleMapView.onDestroy()
}
第二次旋转设备导致googleMapView.onDestroy()
行出现UninitializedPropertyAccessException-MapView未初始化。MyMapViewFragment
:代码
class MyMapViewFragment : Fragment(), OnMapReadyCallback {
private lateinit var googleMapView: com.google.android.gms.maps.MapView
private lateinit var googleMap : GoogleMap
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_map_view, container, false)
googleMapView = view.findViewById(R.id.mapView2)
googleMapView.onCreate(savedInstanceState)
googleMapView.getMapAsync(this)
bottomNavigationView = requireActivity().findViewById(R.id.bottomNavigation)
mountainCustomInfoWindow.visibility = View.INVISIBLE
bottomNavigationView.visibility = View.VISIBLE
return view
}
override fun onMapReady(map: GoogleMap) {
googleMap = map
}
override fun onPause() {
super.onPause()
googleMapView.onPause()
}
override fun onResume() {
super.onResume()
googleMapView.onResume()
}
override fun onDestroy() {
super.onDestroy()
googleMapView.onDestroy()
}
override fun onLowMemory() {
super.onLowMemory()
googleMapView.onLowMemory()
}
}
Stacktrace:
Process: com.hiker, PID: 25388
java.lang.RuntimeException: Unable to destroy activity {com.hiker/com.hiker.presentation.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property googleMapView has not been initialized
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4605)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4623)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4897)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property googleMapView has not been initialized
at com.hiker.presentation.map.MapView.onDestroy(MapView.kt:173)
at androidx.fragment.app.Fragment.performDestroy(Fragment.java:2830)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1028)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1310)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchDestroy(FragmentManagerImpl.java:2644)
at androidx.fragment.app.Fragment.performDestroy(Fragment.java:2825)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1028)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchDestroy(FragmentManagerImpl.java:2644)
at androidx.fragment.app.FragmentController.dispatchDestroy(FragmentController.java:329)
at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity.java:366)
at androidx.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity.java:210)
at android.app.Activity.performDestroy(Activity.java:7479)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1255)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4592)
更多猜测:lateinit
的属性需要在对其执行任何其他操作之前初始化该属性。在这种情况下,我们使用它,因为我们只能在onCreateView
中初始化它,而不能在构造函数中初始化它。您实际上正在初始化onCreateView
的第二行上的googleMapView
。
但是,如果view.findViewById(R.id.mapView2)
返回null
,那么实际上可能没有正确初始化它。如果在布局fragment_map_view
中没有id为mapView2
的元素,则是这种情况。因此,验证那里是否真的有MapView。
另一种可能发生的情况是,如果片段实际上没有附加到视图层次结构,而是在活动中实例化和注册的。当"活动"被销毁时,它销毁了Fragment并对未初始化的对象调用了一个方法此处的实际情况
(决议见评论(。