摘要:
当我尝试将自定义模型发送到存储库时,MutableLiveData为null。我认为这是因为MutableLiveData的观察者。
请读到最后。
ViewModel
class LoginViewModel @Inject constructor(
private val repository: MyRepository) : ViewModel() {
var loginModel: MutableLiveData<LoginModel>
init {
loginModel = MutableLiveData<LoginModel>()
}
fun loadUser(): LiveData<Response<Custom<Token>>> {
return repository.login(loginModel.value!!)
}
}
如您所见,这里有一个MutableLiveData
我的 LoginModel 是这样的
data class LoginModel(var user : String, var password : String)
使用数据绑定和上面的ViewModel绑定来定义片段。
login_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.app.example.view.BaseActivity">
<data>
<variable
name="viewModel"
type="com.app.example.view.login.LoginViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/activityMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_login">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="80dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="80dp"
app:cardCornerRadius="7dp"
app:cardElevation="22dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="60dp"
android:text="@string/bienvenido_text"
android:textAllCaps="true"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:cursorVisible="true"
android:text="@{viewModel.loginModel.user}"
android:gravity="center|start|bottom"
android:hint="@string/email_text"
android:inputType="textEmailAddress"
android:maxLength="50"
android:paddingBottom="10dp"
android:textSize="18sp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:cursorVisible="true"
android:gravity="center|start|bottom"
android:hint="@string/contrasenia_text"
android:text="@{viewModel.loginModel.password}"
android:inputType="textPassword"
android:maxLength="50"
android:paddingBottom="10dp"
android:textSize="18sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btnServerLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:padding="10dp"
android:text="@string/ingresar_text"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="40dp"
android:orientation="horizontal">
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
</layout>
使用数据绑定,我已经实现了 etEmail 和Model UserModel的属性 user 之间的关系,并且还实现了 etPassword < / strong>和属性密码
当用户单击 btnServerLogin 时, LoginFragment 将执行以下代码
binding.btnServerLogin.setOnClickListener {
loginViewModel!!.loadUser().observe(this, Observer<Response<Custom<Token>>> { this.handleResponse(it) })
}
这是问题所在。
kotlin.KotlinNullPointerException
有原因是因为loginModel.value!为空
fun loadUser(): LiveData<Response<Custom<Token>>> {
return repository.login(loginModel.value!!)
}
MutableLiveData的值始终为null。我虽然会在您输入电子邮件或密码的EditText的同时更改它。
这是LoginFragment OnActivityCreated的代码,在其中初始化了ViewModel
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setHasOptionsMenu(true)
DeviceUtils.setTranslucentStatusBar(activity!!.window, R.color.colorPrimaryDark)
loginViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(LoginViewModel::class.java)
binding.setLifecycleOwner(this)
binding.viewModel = loginViewModel
binding.btnServerLogin.setOnClickListener {
mPostsViewModel!!.loadUser().observe(this, Observer<Response<RespuestaModel<JwtTokenModel>>> { this.handleResponse(it) })
}
return
}
我在做什么错了?我需要添加特定的观察者吗?
答案 0 :(得分:1)
由于@communistWatermelon,我能够解决部分问题,但还不够。这是完整的解决方案:
一方面,我们必须像@communistWatermelon所说的那样初始化MutableLiveData的value属性:
var loginModel: MutableLiveData<LoginModel> = MutableLiveData()
init {
loginModel.value = LoginModel()
}
另一方面,我们需要在布局中正确设置绑定
在@ {variable.field}的情况下,活页夹将通过调用variable.getField()生成一段代码来获取值。请注意,如果未提供,则活页夹会隐式为“ get”单词加上前缀。所以@ {variable.getField()}是一个明确的有效选项。
在@ = {variable.field}的情况下,活页夹将创建与以前相同的代码,但是具有用于从View获取值并将其设置回对象的其他代码
阅读后,我们需要更改绑定方式
android:text="@{viewModel.loginModel.password}"
android:text="@{viewModel.loginModel.user}"
到
android:text="@={viewModel.loginModel.password}"
android:text="@={viewModel.loginModel.user}"
良好的编码!
答案 1 :(得分:0)
MutableLiveData<LoginModel>()
不会初始化value
对象的MutableLiveData
属性。您可能需要在init
调用中设置一个值,如下所示:
init {
loginModel = MutableLiveData<LoginModel>()
loginModel.value = LoginModel()
}
因为您没有设置值,否则将导致`NullPointerException at
return repository.login(loginModel.value!!)
答案 2 :(得分:0)
这也发生在我身上,除了发生的事情是我在ViewModel上使用MutableLiveData而不将片段/活动作为LifeCycleOwner注册到ViewDataBinding。
该问题不会遇到此问题,因为
binding.setLifecycleOwner(this)
这可能与问题无关,但是如果其他人像我一样落在这里,请仔细检查您是否对绑定执行setLifecycleOwner。