lateinit属性尚未初始化 [英] lateinit property has not been initialized

查看:629
本文介绍了lateinit属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的linearlayout类,当我想创建此类的实例时,出现错误 lateinit属性尚未初始化 我正在使用最新版本的Butterknife库

I have a custom linearlayout class and when I want to create instance of this class, I got an error lateinit property has not been initialized I'm using the latest version of butterknife library

这是我的科特琳课

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
}
}

这是错误日志

    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)

推荐答案

如果您不想使用任何第三方库,则可以添加这些扩展功能(我倾向于在上下文中使用ContextExtensions.ktViewExtensions.kt或查看相关的扩展功能),然后将其放入

If you don't want to use any thirdparty libraries, you can add these extension functions (I tend to have a ContextExtensions.kt or ViewExtensions.kt for Context or View related extension functions), then put in it

inline fun <reified T : View> View.find(id: Int): T = findViewById(id) as T
inline fun <reified T : View> Activity.find(id: Int): T = findViewById(id) as T
inline fun <reified T : View> Fragment.find(id: Int): T = view?.findViewById(id) as T

这些可以让您从ActivityFragmentView中调用find. 因此,在您的班级内部,而不是

these let you call find from within Activity, Fragment, and Views. So inside your class instead of

@BindView(R.id.menu_title_text_view_id) lateinit var menuTitleTextView : CTextBasic

你可以拥有

val menuTitleTextView by lazy { find<CTextBasic>(R.id.menu_title_text_view_id) }

对于UI之类的东西,最好在不需要更改时最好使用val而不是var.作为编程的一般规则,请尝试使事物尽可能保持不变,您将得到的错误要少得多.

For things like UIs, it's better to val instead of var when they don't need to change. As a general rule in programming, try to keep things as immutable as possible, you would get far less bugs.

这篇关于lateinit属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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