-1
我尝试将设备地理位置转换为片段并使用回调。但集后一次链接是空如何在Kotlin中设置CallBack?
fun updateLocation(location:LatLng){
Log.d(TAG,"Update fragment "+ localUpdater+" "+location)
localUpdater?.tryOutLocation(location)
}
private inner class Receiver: BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent?) {
val bundle = intent?.extras!![AppConstants.LOCATION_BUNDLE] as Bundle
val location = bundle.get(AppConstants.LOCATION_BUNDLE) as LatLng
updateLocation(location)
}
}
fun setUpdater(updater: IUpdateLocation){
this.localUpdater = updater
Log.d(TAG,"Update fragment1 "+localUpdater)
}
我声明回调:
private var localUpdater: IUpdateLocation? = null
代码接口:
interface IUpdateLocation {
fun tryOutLocation(location:LatLng)
}
片段代码:
override fun onActivityCreated(savedInstanceState: Bundle?) {
act?.setUpdater(this)
super.onActivityCreated(savedInstanceState)
}
override fun tryOutLocation(location: LatLng) {
Log.d(TAG,"Update fragment map fragment "+location+" ")
if(gMap != null){
gMap?.uiSettings?.isZoomGesturesEnabled
gMap?.addMarker(MarkerOptions().position(location).title("Test"))
gMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 20.0f))
}
第一次callback is set and work (log)
但第一次调用链接后为空。为什么?我如何解决这个问题?
好,如果它第一次工作,这意味着它没有进入第二次onReceive方法,它不调用updateLocation方法。这意味着你没有以正确的方式发送广播。我猜你没有把它放在onResume方法中,因为onCreate只被调用一次,这就是为什么它只能在第一次。我只是猜测:) – Nenco
什么是'行为'?你确定'act'在'onActivityCreated'中不为null吗? – Naetmul
你在说什么'link'? – chandil03