无法使用Kotlin Android扩展调用getMapAsync函数
我的布局文件是 –
我从MapActivity调用mapFragment.getMapAsync(this),但该函数不适用于此variables。 但以旧的方式使用findViewById工作正常。
val mapFragment = getSupportFragmentManager() .findFragmentById(R.id.map) as SupportMapFragment
现在我想知道如何从ID字段直接调用getMapAsync函数作为Kotlin Android扩展正在为我的布局中的其他视图。
任何帮助表示赞赏。
如果您使用的是android.support.v4.app.Fragment
而不是Activity
那么您可以在onCreateView()
使用OnMapReadyCallback
val mapFragment: SupportMapFragment? = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? mapFragment?.getMapAsync(this)
这是我的XML相同
使用综合属性时,variables将转换为布局中的视图的types。 在这种情况下,types是Fragment
。
如果您仍想使用合成属性,则在调用getMapAsync()
之前,需要将其转换为SupportMapFragment
。 你可以使用像这样的东西:
(mapFragment as? SupportMapFragment)?.getMapAsync(this)
或者先将它分配给另一个variables
val supportMapFragment = mapFragment as? SupportMapFragment supportMapFragment?.getMapAsync(this)