android-fragments 未在片段中初始化属性Map

ctehm74n  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(180)

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)
            //...
     }
  }
  //...
}
6qfn3psc

6qfn3psc1#

我打电话给onMapReadyonMapReady感谢阿玛尔阿卜杜拉

override fun onMapReady(googleMap: GoogleMap) {
       map = googleMap
       configureMap(//...)
   }
  
  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)
            //...
     }
  }

相关问题