我有一个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
}
但上面写着我的";位置";是未解析的引用,它是什么?我该怎么解决?
您可以将位置作为参数传递到构造函数中:
inner class WeatherTask(var location: Location) : AsyncTask<String, Void, String>() {
称之为:
WeatherTask(location).execute()