kotlin-lateinit

Kotin lateinit Var Not initialized - Android Studio

跟風遠走 提交于 2021-02-11 14:02:31
问题 I am working on a simple contact app that stores the contact's name, email, and number. Language: Kotlin Architecture: MVVM But I am getting an error: lateinit property addContactViewModel has not been initialized Activity: class AddContact : AppCompatActivity() { private lateinit var addContactViewModel : AddContactViewModel companion object{ const val EXTRA_REPLY = "com.room.contacts.REPLY" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding

lateinitVar cannot be resolved to use isInitialized from Kotlin 1.2.10

扶醉桌前 提交于 2020-06-16 06:18:12
问题 I want to use this feature the simplest thing like in the example does not work for me: lateinit val foo = 1 val bar = foo::lateinitVar.isInitialized() But I am getting unresolved reference lateinitVar I am using Kotlin 1.2.10 via gradle in Android-Studio (also the Kotlin 1.2.10 plugin installed there) 回答1: There’s no variable with name lateinitVar in your code, thus the error. Look at the example again: this::lateinitVar.isInitialized There’s a variable lateinitVar defined in this , which

Kotlin: lateinit to val, or, alternatively, a var that can set once

♀尐吖头ヾ 提交于 2020-05-09 19:04:41
问题 Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it. Specifically, I wish I had: private lateinit val controlObj:SomeView or: private val controlObj:SomeView by lazy { view:View->view.findViewById(...)} and then: override fun onCreateView(....) { val view = inflate(....) controlObj = view.findViewById(...) or in the 2nd case controlObj.initWith(view) or

How to uninitialize lateinit in Kotlin

送分小仙女□ 提交于 2020-04-15 03:45:33
问题 I have a lateinit var as lateinit var someVariable: SomeVariable I initialize this value like someVariable = SomeVariable() and use it whenever I need. At a certain point, I want to set everything to default and want to "uninitialize" someVariable . How can I do that? I am not looking for changing its type to a nullable object and set null. I need to keep it a Non-Null type. 回答1: I don't think it's possible without some kind of wrapper (or reflection, but about in a moment). In fact, lateinit

How to inject primitive variables in Kotlin?

吃可爱长大的小学妹 提交于 2020-03-13 05:57:47
问题 I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine: @field:[Inject ApplicationContext] lateinit var context: Context but, lateinit modifier is not allowed on primitive type properties in Kotlin (for instance Boolean ), how can I do something like this? @field:[Inject Named("isDemo")] lateinit var isDemo: Boolean when I remove lateinit from this code I get this error Dagger does not support injection into private fields 回答1: First,

How to inject primitive variables in Kotlin?

旧城冷巷雨未停 提交于 2020-03-13 05:56:58
问题 I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine: @field:[Inject ApplicationContext] lateinit var context: Context but, lateinit modifier is not allowed on primitive type properties in Kotlin (for instance Boolean ), how can I do something like this? @field:[Inject Named("isDemo")] lateinit var isDemo: Boolean when I remove lateinit from this code I get this error Dagger does not support injection into private fields 回答1: First,

How to inject primitive variables in Kotlin?

*爱你&永不变心* 提交于 2020-03-13 05:56:03
问题 I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine: @field:[Inject ApplicationContext] lateinit var context: Context but, lateinit modifier is not allowed on primitive type properties in Kotlin (for instance Boolean ), how can I do something like this? @field:[Inject Named("isDemo")] lateinit var isDemo: Boolean when I remove lateinit from this code I get this error Dagger does not support injection into private fields 回答1: First,

isInitialized - Backing field of lateinit var is not accessible at this point

笑着哭i 提交于 2020-02-20 06:20:13
问题 I'm trying to check if a lateinit property has been initialized. In Kotlin 1.2 we now have the isInitialized method for that. It works when I do that in the class where the lateinit property is declared. But when I try to call this from another class I get the following warning: Backing field of 'lateinit var foo: Bar' is not accessible at this point My model class (let's say Person ) is written in Java Two other classes (let's say Test1 and Test2 ) are written in Kotlin Example: class Test1