有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

kotlin如何解决java。lang.IllegalArgumentException

当我在kotlin customadapter中编写此代码时

它总是因以下错误而崩溃:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter listCountry

完成的代码:

自定义适配器。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)
    }

}

主要活动。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()
        }
    })
}
}

共 (2) 个答案

  1. # 1 楼答案

    这是因为response和countryList可以为null:response!!。body()。数据国家名单 你必须检查一下

    response?.body().data.countryList?.let {
            recyclerList.layoutManager = LinearLayoutManager(this@MainActivity)
            recyclerList.adapter = CustomAdapter(this@MainActivity, it)
        }
    

    用法!!因为nullable是一种糟糕的做法

  2. # 2 楼答案

    您的适配器有一个构造函数,它接受一个非空的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

    这两件事中有一件可能会出问题:

    • 如果这里的模型类是用Java编写的,那么这个countryList将有一个platform type,因此由您决定是否将其视为可空
    • 即使您的模型是用Kotlin编写的,并且其中的countryList属性不可为空,如果您使用使用使用反射来实例化模型的序列化工具(例如Gson),那么null值仍然可以写入该属性,如果网络响应的是该属性

    至于解决方案:将此列表显式地键入为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
    }