【发布时间】:2020-10-04 12:50:42
【问题描述】:
我有这个 getLastLocation 函数,它获取当前用户位置并将它们存储到 location.latitude 和 location.longitude
fun getLastLocation(){
...
Log.d("Debug:" ,"Latitude: "+ location.latitude+" Longitude: "+location.longitude)
...
}
我有这个内部类,它通过 lat 和 lon 值从 openweathermap 获取天气:
inner class WeatherTask : AsyncTask<String, Void, String>() {
override fun doInBackground(vararg params: String?): String? {
var latitude = location.latitude
var longitude = location.longitude
var response: String?
try {
response =
URL("http://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&units=metric&appid=$api").readText(
Charsets.UTF_8
)
} catch (e: Exception) {
response = null
}
return response
}
但它说我的“位置”是一个未解决的参考,它是什么?我该如何解决?
【问题讨论】:
-
location是你外部类的属性吗? -
@Nicolas 是在 getLastLocation() 函数中声明的,但是我跳过了这里是完整的代码:shorturl.at/glADJ
-
啊,你不能在其函数范围之外使用局部变量。
标签: android kotlin location global-variables