【发布时间】:2019-02-15 09:03:16
【问题描述】:
当我在 kotlin customadapter 中编写这段代码时
它总是崩溃并出现以下错误:
java.lang.IllegalArgumentException:指定为非空的参数是 null:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, 参数列表国家
完成的代码:
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()
}
})
}
}
【问题讨论】: