指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数convertView [英] Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter 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.
这意味着框架将convertView
的null
值传递给此方法是完全有效的(意味着您需要创建一个新视图并将其返回,而不是回收旧视图).
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屋!