GoogleMap未在片段中初始化。即使尝试在不同的片段生命周期中调用getMapAsync
class MyMapFragment : Fragment(), OnMapReadyCallback {
private lateinit var map: GoogleMap
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
//...binding
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.r_map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
//...
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
}
private fun configureMap(
start: Pair<Double, Double>,
end: Pair<Double, Double>,
) {
//Here the map says it's not initialized
//so the condition fails
if (this::map.isInitialized) {
drawPolyLine(start.first, start.second, end.first, end.second)
//...
}
}
//...
}
1条答案
按热度按时间6qfn3psc1#
我打电话给
onMapReady
的onMapReady
感谢阿玛尔阿卜杜拉