我正在学习将Kotlin编码为一种爱好,但是我碰壁了。我收到以下错误:
None of the following functions can be called with the arguments supplied:
public constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: Array<(out) TypeVariable(T)!>) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int) defined in android.widget.ArrayAdapter
public constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, objects: (Mutable)List<TypeVariable(T)!>) defined in android.widget.ArrayAdapter
直到我从openweather添加API提取一些数据以来,一切都很好。任何帮助表示赞赏!
这是我的代码:
package com.example.myweatherapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class ForecastActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_forecast)
var retriever = WeatherRetriever()
val callback = object : Callback<Weather> {
override fun onFailure(call: Call<Weather>?, t: Throwable) {
println("It failed")
}
override fun onResponse(
call: Call<Weather>?, response: Response<Weather>?) {
println("It wORKED")
println(response?.body()?.main)
title = response?.body()?.name
var forecasts = response?.body()?.main
var castListView = findViewById<ListView>(R.id.forecastListView)
var adapter = ArrayAdapter(this@ForecastActivity,android.R.layout.simple_list_item_1,forecasts)
castListView.adapter=adapter
}
}
retriever.getForecast(callback)
}
}
ArrayAdapter(this@ForecastActivity,android.R.layout.simple_list_item_1,forecasts)
ArrayAdapter要求对象的Array / List作为第三个参数,但是forecasts
只是响应主体。
See more details here about ArrayAdapter constructors
您需要解析响应并准备对象的列表/数组(即字符串标题)以作为ArrayAdapter构造函数中的第3个参数传递。