错误:错误:在类型为View的可为空的接收方上只允许使用safe(?。)或非null断言(!!)调用。
我想从Java代码到kotlin的一些代码,但我不断gettin foll错误
错误:错误:在类型为View的可为空的接收方上只允许使用safe(?。)或非null断言(!!)调用。
Java代码
View listItemView = convertView; if (listItemView == null) { listItemView = LayoutInflater.from(getContext()).inflate( R.layout.list_item, parent, false); } // Get the {@link Word} object located at this position in the list Word currentWord = getItem(position); // Find the TextView in the list_item.xml layout with the ID miwok_text_view. TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
转换成kotlin后
var listView:View? =convertView if(listView==null){ listView=LayoutInflater.from(context).inflate(R.layout.list_item,parent,false) } var currentWord:Word=getItem(position) val miwokTextView= listView.findViewById(R.id.miwok_text_view) as TextView
我在listView.findViewById错误,即使包括? 或!!,错误不会消失。我甚至尝试从JetBrains在线转换器,当我把转换后的代码粘贴到android studio,我仍然不断得到error.Please帮助
我尝试使用val miwokTextView= listView?.findViewById(R.id.miwok_text_view) as TextView
和val miwokTextView= listView!!.findViewById(R.id.miwok_text_view) as TextView
但我仍然在findViewById
得到它val miwokTextView = listView?.findViewById <
View >
(R.id.miwok_text_view)as TextView
将val
类型更改为var
,如此格式。因为val不能被重新分配。
var miwokTextView= listView?.findViewById<View>(R.id.miwok_text_view) as TextView