指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数convertView [英] Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView

查看:1362
本文介绍了指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数convertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将适配器代码转换为Kotlin之后,我得到了此错误:

I got this error just after converted the adapter code to Kotlin:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
    at ...MyAdapter.getView(Unknown Source:35)
    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
    at android.widget.AbsListView.obtainView(AbsListView.java:2366)

膨胀行时会引发错误:

class LegalAdapter internal constructor(private val activity: Activity, private val list: ArrayList<Item>) : BaseAdapter() {

    override fun getView(position: Int, convertView: View, parent: ViewGroup): View {

    val layoutInflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    @SuppressLint("ViewHolder")
    val row = layoutInflater.inflate(R.layout.legal_list_item, parent, false) //exception is throw here

显然,某些不应该为null的参数为null,kotlin会对其进行检查.问题是我什至无法调试新的kotlin代码.

Apparently, some parameter that shouldn't be null is null, and kotlin check it. Problem is i can't even debug the new kotlin code.

推荐答案

getView()方法是Adapter接口的一部分,并且是用Java定义的. 此处的文档.重要的部分是有关convertView参数的注释:

The getView() method is a part of the Adapter interface, and is defined in Java. Documentation here. The important part is this note about the convertView parameter:

视图:要重用的旧视图(如果可能).注意:使用前,应检查此视图是否为非null并具有适当的类型.

View: The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using.

这意味着框架将convertViewnull值传递给此方法是完全有效的(意味着您需要创建一个新视图并将其返回,而不是回收旧视图).

This means that it's quite valid for the framework to pass null values for convertView to this method (meaning that you need to create a new view and return that, rather than recycling an old view).

反过来,这意味着convertView的Kotlin定义必须为View?类型,而不仅仅是View.因此,将函数签名更改为:

In turn, this means that the Kotlin definition of convertView must be of type View?, not just View. So change your function signature to this:

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View

这篇关于指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数convertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆