Ich versuche, Gerät Geolocation in Fragment zu übertragen und Callback zu verwenden. Aber nach dem Satz einer Zeit Link ist nullWie setze ich CallBack in Kotlin?
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)
}
Ich erkläre Rückruf:
private var localUpdater: IUpdateLocation? = null
-Code-Schnittstelle:
interface IUpdateLocation {
fun tryOutLocation(location:LatLng)
}
Fragment Code:
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))
}
Zum ersten Mal callback is set and work (log)
aber nach dem ersten Aufruf Link ist null. Warum? Wie kann ich dieses Problem beheben?
Nun, wenn es das erste Mal funktioniert, bedeutet dies, dass es die Methode onReceive nicht zum zweiten Mal aufruft und die updateLocation-Methode nicht aufruft. Das bedeutet, dass Sie eine Sendung nicht richtig senden. Ich nehme an, dass Sie es nicht in die onResume-Methode geschrieben haben, da onCreate nur einmal aufgerufen wird, weshalb es nur beim ersten Mal funktioniert. Ich rate nur hier :) – Nenco
Was ist 'act'? Haben Sie sichergestellt, dass "act" in "onActivityCreated" nicht null ist? – Naetmul
über welche "link" sprichst du? – chandil03