我的ViewModel实现了可序列化,并且我已经定义了按钮单击方法,如下所示。
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>现在,在文本视图上单击我正在获取的
com.aman.camellia.kniterider.viewmodel.FragmentHomeViewModel):Parcelable遇到了写入可序列化对象的IOException (名称=java.lang.RuntimeException)
Caused by: java.io.NotSerializableException: retrofit2.Retrofit$1我认为这个错误的原因可能是我使用的修改(实际上我用匕首)。
发布于 2018-02-10 09:16:24
好的,我终于得到了答案。造成此问题的原因是,我的视图模型实现了Serializable和reason实例没有序列化,所以要么需要序列化改造,要么是将它作为临时变量使其不被序列化。在那之后我的视图模型将是..。
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");
}
} 另一项活动成功地开始了。
https://stackoverflow.com/questions/48717880
复制相似问题