属性图未在片段中初始化
Google地图没有在片段中初始化。即使在不同的片段生命周期中尝试调用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)
//...
}
}
//...
}
Google map is not being initialized in fragment. Even after trying to call getMapAsync
in different fragment lifecycles
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)
//...
}
}
//...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在
onmapready
上打了configureMap
,感谢 Ammar AbdullahI called the
configureMap
on theonMapReady
thanks to Ammar Abdullah