Kotlin lateinit Var 未初始化 - Android Studio
Kotin lateinit Var Not initialized - Android Studio
我正在开发一个简单的联系人应用程序,用于存储联系人的姓名、电子邮件和电话号码。
语言:科特林
架构:MVVM
但是我收到一个错误: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 = DataBindingUtil.setContentView<ActivityAddContactBinding>(
this,R.layout.activity_add_contact
)
binding.btnSave.setOnClickListener {
val replyIntent = Intent()
val fname= binding.fnameEdit.text.toString()
val lname= binding.lnameEdit.text.toString()
val email = binding.emailAddressEdit.text.toString()
val contactno = binding.contactNumberEdit.text.toString()
val contact = Contact(fname, lname,email,contactno)
addContactViewModel.insert(contact)
val intent = Intent(this, ContactList::class.java)
// start your next activity
startActivity(intent)
}
}}
ViewModel:
class AddContactViewModel (application: Application) : AndroidViewModel(application){
private val repository: ContactRepository
init {
val contactsDao= ContactRoomDatabase.getDatabase(application).contactDao()
repository = ContactRepository(contactsDao)
}
fun insert(contact: Contact) = viewModelScope.launch {
repository.insert(contact)
}}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.room.contacts, PID: 29383
kotlin.UninitializedPropertyAccessException: lateinit property addContactViewModel has not been initialized
at com.room.contacts.screens.addContact.AddContact.access$getAddContactViewModel$p(AddContact.kt:15)
at com.room.contacts.screens.addContact.AddContact$onCreate.onClick(AddContact.kt:36)
at android.view.View.performClick(View.java:4781)
at android.view.View$PerformClick.run(View.java:19907)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:5541)
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:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
您可以将 addContactViewModel
设为 lateinit
属性,而不是将其设为 lazy
属性,以便它在第一时间被初始化你尝试在某处使用它:
class AddContact : AppCompatActivity() {
private val addContactViewModel : AddContactViewModel by lazy {
ViewModelProvider(this).get(AddContactViewModel::class.java)
}
您应该在 oncreate 中初始化 addContactViewModel,而不是在 setOnClickListener 中
我正在开发一个简单的联系人应用程序,用于存储联系人的姓名、电子邮件和电话号码。
语言:科特林
架构:MVVM
但是我收到一个错误: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 = DataBindingUtil.setContentView<ActivityAddContactBinding>(
this,R.layout.activity_add_contact
)
binding.btnSave.setOnClickListener {
val replyIntent = Intent()
val fname= binding.fnameEdit.text.toString()
val lname= binding.lnameEdit.text.toString()
val email = binding.emailAddressEdit.text.toString()
val contactno = binding.contactNumberEdit.text.toString()
val contact = Contact(fname, lname,email,contactno)
addContactViewModel.insert(contact)
val intent = Intent(this, ContactList::class.java)
// start your next activity
startActivity(intent)
}
}}
ViewModel:
class AddContactViewModel (application: Application) : AndroidViewModel(application){
private val repository: ContactRepository
init {
val contactsDao= ContactRoomDatabase.getDatabase(application).contactDao()
repository = ContactRepository(contactsDao)
}
fun insert(contact: Contact) = viewModelScope.launch {
repository.insert(contact)
}}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.room.contacts, PID: 29383
kotlin.UninitializedPropertyAccessException: lateinit property addContactViewModel has not been initialized
at com.room.contacts.screens.addContact.AddContact.access$getAddContactViewModel$p(AddContact.kt:15)
at com.room.contacts.screens.addContact.AddContact$onCreate.onClick(AddContact.kt:36)
at android.view.View.performClick(View.java:4781)
at android.view.View$PerformClick.run(View.java:19907)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:5541)
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:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
您可以将 addContactViewModel
设为 lateinit
属性,而不是将其设为 lazy
属性,以便它在第一时间被初始化你尝试在某处使用它:
class AddContact : AppCompatActivity() {
private val addContactViewModel : AddContactViewModel by lazy {
ViewModelProvider(this).get(AddContactViewModel::class.java)
}
您应该在 oncreate 中初始化 addContactViewModel,而不是在 setOnClickListener 中