In my activity I have a field that should be non-nullable and has a custom setter. I want to initialize the field in my onCreate
method so I added lateinit
to my variable declaration. But, apparently you cannot do that (at the moment): https://discuss.kotlinlang.org/t/lateinit-modifier-is-not-allowed-on-custom-setter/1999.
These are the workarounds I can see:
- Do it the Java way. Make the field nullable and initialize it with null. I don't want to do that.
- 使用该类型的"默认实例"初始化该字段.我现在就是这么做的.但对某些型号来说太贵了.
有人能推荐一个更好的方法(不需要删除自定义设置器)吗?