我正在使用Android中requestLocationUpdates()
中的FusedLocationProvider
,并将其设置为LiveData
和ViewModel
。这是我的代码:
LocationLiveData.kt
class LocationLiveData(context: Context) : LiveData<Location?>() {
private val fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(context)
private val locationCallback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
super.onLocationResult(p0)
p0 ?: return
for (loc in p0.locations) {
value = loc
}
}
}
companion object {
val locationRequest: LocationRequest = LocationRequest.create().apply {
interval = 60000
fastestInterval = 10000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
}
override fun onActive() {
super.onActive()
fusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location? ->
location?.also {
value = location
}
}.addOnFailureListener { e: Exception ->
Log.d("LocationLiveData", e.message ?: "FusedProviderClient Failed!")
value = null
}
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
locationCallback,
null
)
}
override fun onInactive() {
super.onInactive()
fusedLocationProviderClient.removeLocationUpdates(locationCallback)
}
}
我正在像这样的片段中观察它:
userAuthVM.locationUpdates.observe(viewLifecycleOwner, Observer {
val geocoder = Geocoder(context, Locale.getDefault())
if (it != null) {
userLoc = geocoder.getFromLocation(it.latitude, it.longitude, 1)[0]
}
})
有时我会收到一个我不太了解的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.halalrishtey, PID: 14048
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.io.IOException: grpc failed
at android.location.Geocoder.getFromLocation(Geocoder.java:136)
at com.halalrishtey.RegisterFragment$onViewCreated$1.onChanged(RegisterFragment.kt:35)
at com.halalrishtey.RegisterFragment$onViewCreated$1.onChanged(RegisterFragment.kt:19)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:131)
at androidx.lifecycle.LiveData.setValue(LiveData.java:289)
at com.halalrishtey.viewmodels.LocationLiveData.access$setValue(LocationLiveData.kt:13)
at com.halalrishtey.viewmodels.LocationLiveData$onActive$1.onSuccess(LocationLiveData.kt:40)
at com.halalrishtey.viewmodels.LocationLiveData$onActive$1.onSuccess(LocationLiveData.kt:13)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我在做什么错了?