I have a custom linearlayout class and when I want to create instance of this class, I get the following error:
lateinit property has not been initialized
I'm using the latest version of butterknife library.
The following is my Kotlin class:
class MenuItemView : LinearLayout {
@BindView(R.id.menu_title_text_view_id)
lateinit var menuTitleTextView : CTextBasic
constructor(ctx: Context) : super(ctx) {
}
init {
val view = LayoutInflater.from(context).inflate(R.layout.menu_item,this)
ButterKnife.bind(this,view)
}
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
val menuAttrs = context.theme.obtainStyledAttributes(attrs, R.styleable.MenuItemView, 0, 0)
try {
val title: String = menuAttrs.getString(R.styleable.MenuItemView_menu_title)
menuTitleTextView.text = title
}catch (e : Exception){
e.printStackTrace()
}finally {
menuAttrs.recycle()
}
}
fun setTitle( title : String){
menuTitleTextView.text = title
}
}
Error Log
kotlin.UninitializedPropertyAccessException: lateinit property menuTitleTextView has not been initialized
at com.leavigstone.liberali.ui.custom.menu.MenuItemView.setTitle(MenuItemView.kt:48)
at com.leavigstone.liberali.ui.activities.MainActivity.onAddButtonClick(MainActivity.java:142)
at com.leavigstone.liberali.ui.activities.MainActivity_ViewBinding$3.doClick(MainActivity_ViewBinding.java:54)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…