我有一个基类'TestImage',它有一个'State'类型的具体实现实例变量'urlImageState'
public class TestImage implements Parcelable {
private State urlImageState, state;
private TestImage(Parcel in) {
urlImageState = in.readParcelable(UrlImageState.class.getClassLoader());
}
public static final Creator<TestImage> CREATOR = new Creator<TestImage>() {
@Override
public TestImage createFromParcel(Parcel in) {
return new TestImage(in);
}
@Override
public TestImage[] newArray(int size) {
return new TestImage[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(urlImageState, flags);
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
}
UrlImageState类:
public class UrlImageState implements Parcelable, State {
private TestImage image;
private OwnerContract.ViewModel ownerViewModel;
public UrlImageState(OwnerContract.ViewModel viewModel, TestImage image) {
this.ownerViewModel = viewModel;
this.image = image;
}
private UrlImageState(Parcel in) {
}
public static final Creator<UrlImageState> CREATOR = new Creator<UrlImageState>() {
@Override
public UrlImageState createFromParcel(Parcel in) {
return new UrlImageState(in);
}
@Override
public UrlImageState[] newArray(int size) {
return new UrlImageState[size];
}
};
@Override
public void loadImage() {
Log.e("", "UrlImageState");
this.ownerViewModel.loadImageUrl(image.getUrl());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
'州'界面
public interface State extends Parcelable {
void loadImage();
}
在'onSaveInstanceState'方法内部,当日志显示时,TestImage状态的类型为'UrlImageState'。
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e(debugTag, image.getState() + " onSaveInstanceState");
outState.putParcelable("image", image);
}
问题出在onCreate里面我没有得到状态(空指针异常)。我的parcelable实现有些不对劲。有人可以帮忙??