問題一
java中自定義控件的必備構造函數三賤客(Context context, AttributeSet attrs, int defStyleAttr),具體功能不詳述了,
kotlin中構造函數,舉個栗子
class CircleView (context: Context, attrs: AttributeSet? = null,defStyle: Int = android.R.attr.editTextStyle) : View(context, attrs, defStyle)
系統會優先推薦這種集成好的,所以問題來了,接下來我要裝個逼了,
運行後給你報個錯
Caused by: android.view.InflateException: Binary XML file line #53: Binary XML file line #53: Error inflating class com.xx.xxx.lib_component.editText.CircleView
Caused by: android.view.InflateException: Binary XML file line #53: Error inflating class com.xx.xxx.lib_component.editText.CircleView
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
網上也有告訴你是少了那三賤客,所以你就懵逼了,明明給了(Context context, AttributeSet attrs, int defStyleAttr),
然後你一頓瞎操作你就會發現你需要老老實實的寫三個構造
constructor(context: Context) : this(context, null, 0)
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAtt) {
initView(attrs,defStyleAttr)
}
然後就木有問題了,
當然還有個騷操作,構造前加上@JvmOverloads,你就可一直這樣騷
class ClearEditText @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null,
defStyle: Int = android.R.attr.editTextStyle
) : EditText(context, attrs, defStyle), View.OnFocusChangeListener, TextWatcher {
init {
initView()
}
好了,有問題在記錄
優秀的分割線------------------------------------------------------------------------------
問題2
做了一個簡單的listView適配,所以直接選擇了繼承BaseAdapter(),快捷實現裏面的方法後,看上去一切都完美
然後給我報了個看不到的錯誤
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
at xxx.xt.tlq.com.widget.blurView.MyListAdapter.getView(Unknown Source:2)
......
......
然後我安照提示點了getView(Unknown Source:2),就尼瑪給我跳到這裏來了,
由於自動導入的getView是沒有非空的形參
override fun getView(position: Int, convertView: View, parent: ViewGroup): View {
return null
}
然後查到是convertView的null異常,受益於只要在View後面加個"?"就好,