当我用kotlin customadapter编写此代码时
它总是崩溃并显示以下错误:
java.lang.IllegalArgumentException:指定为非null的参数是 null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, 参数listCountry
完整的代码:
CustomAdapter.kt
class CustomAdapter(var context: Context,val listCountry: List<Model.Country>) : RecyclerView.Adapter<CustomAdapter.CustomHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, position: Int): CustomHolder {
val view = LayoutInflater.from(context).inflate(R.layout.custom_recycler,parent,false)
return CustomHolder(view)
}
override fun getItemCount(): Int = listCountry.size
override fun onBindViewHolder(holder: CustomHolder, position: Int) {
val flag = listCountry[position]
Glide.with(context).load(flag.image).into(holder.countryFlag)
holder.numCode.text = flag.numcode
holder.countryName.text = flag.name
}
class CustomHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val numCode : TextView = itemView.findViewById(R.id.tvCountryCode)
val countryName : TextView = itemView.findViewById(R.id.tvCountryName)
val countryFlag : ImageView = itemView.findViewById(R.id.ivCountryFlag)
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val apiservice = RetrofitClient().getClient()!!.create(RetrofitApiInterface::class.java)
val call : Call<Model> = apiservice.getData()
call.enqueue(object : Callback<Model>{
override fun onFailure(call: Call<Model>?, t: Throwable?) {
Log.e("Failure",t.toString())
}
override fun onResponse(call: Call<Model>?, response: Response<Model>?) {
Log.e("Response","This is Successfull Response")
val listCountry = response!!.body().data.countryList
recyclerList.layoutManager = LinearLayoutManager(this@MainActivity)
recyclerList.adapter = CustomAdapter(this@MainActivity,listCountry)
//Toast.makeText(this@MainActivity,"Successfull",Toast.LENGTH_SHORT).show()
}
})
}
}
答案 0 :(得分:0)
您的适配器具有此构造函数,该构造函数采用非空的List<Model.Country>
:
class CustomAdapter(var context: Context, val listCountry: List<Model.Country>)
然后从网络响应中获取数据,并将其作为参数传递给构造函数:
val listCountry = response!!.body().data.countryList
...
recyclerList.adapter = CustomAdapter(this@MainActivity, listCountry)
这是您崩溃的地方,因为listCountry
最终包含一个null
值。
有可能这两个问题之一在这里出了错:
countryList
将有一个platform type,因此由您决定是否要将其视为可为空。countryList
属性是不可为空的,如果您使用通过反射实例化模型的序列化工具(例如Gson),则null
值如果那是网络做出的响应,则可能仍会写在该属性中。作为一种解决方案:将该列表显式键入为可为空(如果出现平台类型问题,请在代码中此处,否则在网络模型中),然后执行编译器强制您执行的null检查:>
val listCountry: List<Model.Country>? = response!!.body().data.countryList
...
if (listCountry != null) {
recyclerList.adapter = CustomAdapter(this@MainActivity, listCountry)
} else {
// handle missing part of network response somehow
}
答案 1 :(得分:0)
这是因为response和countryList可以为null:response !!。body()。data.countryList 你必须检查一下。
response?.body().data.countryList?.let {
recyclerList.layoutManager = LinearLayoutManager(this@MainActivity)
recyclerList.adapter = CustomAdapter(this@MainActivity, it)
}
用法!因为可为空是一个坏习惯