我的ViewModel实现了serializable,我已经定义了按钮点击方法,如下所示..
public class FragmentHomeViewModel
extends BaseObservable
implements Serializable {
private Rerofit retrofit;
private HomeScreen activity;
public FragmentHomeViewModel(HomeScreen activity) {
this.activity = activity;
retrofit = MyGlobal.getMyGlobale(activity).getNetComponent().retrofit();
}
//
//....code
public void onClickCuisine(View view){
Intent intent = new Intent(view.getContext(), SelectCuisineActivity.class);
(view.getContext()).startActivity(intent);
Log.e("is clicked"," may be");
}
}
这是我用这个视图模型附加的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"
>
<data class="Item4DataMindingCuisine">
<import type="com.aman.camellia.kniterider.utils.Constrants" />
<variable
name="index"
type="int"/>
<variable
name="viewModel"
type="com.aman.camellia.kniterider.viewmodel.FragmentHomeViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dim6dp"
android:layout_marginLeft="@dimen/dim10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="View All"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/text16sp"
android:onClick="@{viewModel::onClickCuisine}"
app:fontText="@{Constrants.BOLD_FONT}"
/>
</RelativeLayout>
</layout>
现在在textview上点击我正在
java.lang.RuntimeException:Parcelable遇到IOException写入 可序列化对象(name = com.aman.camellia.kniterider.viewmodel.FragmentHomeViewModel)
Caused by: java.io.NotSerializableException: retrofit2.Retrofit$1
知道为什么会出现这个错误。我认为这个错误的原因可能是我使用过的改造(实际上我使用了匕首)。
答案 0 :(得分:2)
好吧最后我得到了答复。 出现这个问题的原因是,我的viewmodel实现了Serializable和retrofit实例没有被序列化,所以你需要序列化改造或者将改进变量作为瞬态变量来进行序列化。 之后我的观点模型将是......
public class FragmentHomeViewModel
extends BaseObservable
implements Serializable {
transient Rerofit retrofit;
private HomeScreen activity;
public FragmentHomeViewModel(HomeScreen activity) {
this.activity = activity;
retrofit = MyGlobal.getMyGlobale(activity).getNetComponent().retrofit();
}
//
//....code
public void onClickCuisine(View view){
Intent intent = new Intent(view.getContext(), SelectCuisineActivity.class);
(view.getContext()).startActivity(intent);
Log.e("is clicked"," may be");
}
}
这样做另一项活动成功开始。