gpt4 book ai didi

android - lateinit 属性性别尚未初始化

转载 作者:行者123 更新时间:2023-12-03 10:54:00 25 4
gpt4 key购买 nike

使用数据绑定(bind)、ViewModel、LiveData ( MVVM )。
在我的布局中有一个表单可以添加员工详细信息。
布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModelAddEmployee"
type="com.app.roomemployeedemo.viewmodel.AddEmployeeViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/edt_lastname"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.AddEmployeeActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:text="@{viewModelAddEmployee.firstname}"
android:id="@+id/edt_firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Jaimin"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:text="@{viewModelAddEmployee.lastname}"
android:id="@+id/tv_lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Modi"
android:inputType="textPersonName"
android:maxLength="30"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_firstname" />
<EditText
android:text="@{viewModelAddEmployee.age}"
android:id="@+id/edt_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="25"
android:inputType="textPersonName"
android:maxLength="3"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_lastname" />
<EditText
android:text="@{viewModelAddEmployee.gender}"
android:id="@+id/edt_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Gender (M/F, m/f)"
android:inputType="textPersonName"
android:maxLength="1"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_age" />
<EditText
android:text="@{viewModelAddEmployee.salary}"
android:id="@+id/edt_salary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Salary (50000)"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_gender" />
<Button
android:onClick="@{(v) -> viewModelAddEmployee.onAddEmployeeClick(v)}"
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="40dp"
android:text="ADD NOW"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_salary" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

下面是相同的 ViewModel 类:
 class AddEmployeeViewModel(mContext: Context) : ViewModel(){
var mContext=mContext
lateinit var firstname:MutableLiveData<String>
lateinit var lastname:MutableLiveData<String>
lateinit var age:MutableLiveData<Int>
lateinit var gender:MutableLiveData<Char>
lateinit var salary:MutableLiveData<Int>
lateinit var repositoryEmployee:EmployeeRepository
init{
repositoryEmployee= EmployeeRepository(mContext)
}
fun onAddEmployeeClick(view: View)
{
repositoryEmployee.onAddEmployeeClick(mContext,firstname,lastname,age,gender,salary)
}
}
Activity 类中初始化的 Viewmodel 和绑定(bind)实用程序如下:
class AddEmployeeActivity : AppCompatActivity() {
lateinit var viewModelAddEmployee: AddEmployeeViewModel
lateinit var binding: ActivityAddEmployeeBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModelAddEmployee = ViewModelProvider(this,AddEmployeeFactory(this@AddEmployeeActivity)).get(AddEmployeeViewModel::class.java)
binding = DataBindingUtil.setContentView(
this@AddEmployeeActivity,
R.layout.activity_add_employee
)
binding?.setLifecycleOwner(this)
binding?.viewModelAddEmployee = viewModelAddEmployee
}
}
但是,出现以下错误:
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property gender has not been initialized
at com.app.roomemployeedemo.viewmodel.AddEmployeeViewModel.getGender(AddEmployeeViewModel.kt:16)
可能是什么问题?尤其是 Char 类型的性别!

最佳答案

在 kotlin 中,您必须初始化 lateinit 属性。将以下行添加到 oncreate -

 gender = MutableLiveData<Char>()

关于android - lateinit 属性性别尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65421822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com