所以我試圖從函式回傳一個值以在 mainActivity 類中使??用,但是當我嘗試回傳變數時出現錯誤,指出“必須初始化變數”,即使我已經給它一個值。對此有什么想法嗎?
private fun getCLocation() : LocationClass {
var loc: LocationClass
if(checkPermission()){
if(isLocationEnabled()){
locationVariable.lastLocation.addOnCompleteListener(this) { task->
val location:Location? = task.result
if(location == null){
Toast.makeText(this,"NULL",Toast.LENGTH_LONG).show()
}
else{
loc = LocationClass(location.latitude.toString(),location.longitude.toString())
}
}
} else {
// location not enabled,open settings
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
} else {
//Permission Not Enabled
requestPermission()
}
return loc
}
錯誤是:必須初始化變數“loc”
uj5u.com熱心網友回復:
您的代碼的兩個問題:
- 用戶可能還沒有授予權限,所以它會進入
else
塊,該函式請求權限,但不做任何事情來設定初始位置值。在這種情況下,該函式不可能回傳有效位置。 - 獲取位置更新是異步的。即使已經授予權限,您的 OnCompleteListener 也不會被呼叫,直到該函式已經回傳之后的某個時間。您可以在此處閱讀有關什么是異步 API 的更多說明。
這是做什么的基本策略。獲取位置的函式采用回呼引數,而不是直接回傳 LocationClass,因為它是異步的。您需要一個可以在位置或權限可用后重試的更高級別的邏輯。
private var triedPromptingLocationSetting = false
private var locationWorkflowInitiated = false // SET THIS BACK TO FALSE IN onViewDestroyed() IF IN FRAGMENT
private fun fetchCLocation(onReceived: (LocationClass?)->Unit) {
if (!checkPermission() || isLocationEnabled()) {
Log.e("Do not call getCLocation() before permission is granted and location is turned on! Ignoring.")
return
}
locationVariable.lastLocation.addOnCompleteListener(this) { task->
val location:Location? = task.result
if (location == null) {
onReceived(null)
}
else {
var loc = LocationClass(location.latitude.toString(),location.longitude.toString())
onReceived(loc)
}
}
}
// This is where the logic is for whatever you wanted to do with this location.
// In this example, it's assumed it would be called in onResume()
private fun doMyLocationWorkflow() {
if (!checkPermission()) {
requestPermission()
return
}
if (!isLocationEnabled()) {
if (triedPromptingLocationSetting) {
// can't infinitely loop back to settings.
showSomeUiRequestingUserToManuallyEnableLocation()
} else {
triedPromptingLocationSetting = true
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
return
}
locationWorkflowInitiated = true
fetchCLocation { location ->
if (location == null) {
Toast.makeText(this,"NULL",Toast.LENGTH_LONG).show()
//...
return@fetchCLocation
}
// Do something with the LocationClass
}
}
override fun onResume() {
super.onResume()
if (!locationWorkflowInitiated) {
doMyLocationWorkflow()
}
}
private val locationPermissionRequest = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
when {
permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
doMyLocationWorkflow() // go back to original workflow
}
permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
doMyLocationWorkflow() // go back to original workflow
} else -> {
showSomeUiTellingUserPermissionMustBeGranted()
}
}
}
private fun requestPermission() {
locationPermissionRequest.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/506969.html
下一篇:Formik條件驗證不起作用