【问题标题】:How to solve java.lang.IllegalArgumentException如何解决 java.lang.IllegalArgumentException
【发布时间】: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()
        }
    })
}
}

【问题讨论】:

    标签: java kotlin


    【解决方案1】:

    你的适配器有这个构造函数,它接受一个非空的List&lt;Model.Country&gt;

    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 值在该属性中,如果这是网络响应的内容。

    至于解决方案:将此列表显式键入为可空(如果出现平台类型问题,请在您的代码中,否则在您的网络模型中),然后执行编译器对您强制执行的空检查:

    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
    }
    

    【讨论】:

      【解决方案2】:

      这是因为 response 和 countryList 可以为空: response!!.body().data.countryList 你必须检查它。

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

      用法!! for nullable 是一种不好的做法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多