我试图将一个Parcelable对象从一个活动发送到另一个活动,但是当我尝试从Intent临时对象中取回该对象时,我得到了:
运行时异常“包裹android.os.Parcel@9a7cc8:解组未知类型代码7274595”
代码:
启动活动
public class Activity1 {
public static void start(Activity currrentActivity, V1Modal modal) {
Intent auIntent = new Intent(currrentActivity, Activity1.class);
if (modal != null) {
auIntent.putExtra(MODAL_DATA_KEY, modal);
}
auIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
currrentActivity.startActivity(auIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_useriq);
if (getIntent().getExtras() != null) {
getIntent().setExtrasClassLoader(V1Modal.class.getClassLoader());
if (getIntent().getExtras().containsKey(MODAL_DATA_KEY)) {
modal = (V1Modal) getIntent().getExtras().getParcelable(MODAL_DATA_KEY);
}
}
}
V1Model类
public class V1Modal implements Parcelable {
public static final Parcelable.Creator<V1Modal> CREATOR = new Parcelable.Creator<V1Modal>() {
@Override
public V1Modal createFromParcel(Parcel source) {
return new V1Modal(source);
}
@Override
public V1Modal[] newArray(int size) {
return new V1Modal[size];
}
};
public String id;
String name;
public Node layout;
private V1Modal() {
}
protected V1Modal(Parcel in) {
this.id = in.readString();
this.name = in.readString();
this.layout = in.readParcelable(UINode.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.name);
dest.writeParcelable(this.layout, flags);
}
}
UiNode类
public abstract class UINode implements Parcelable {
protected UINode(Parcel in) {
this.bgColor = new ArrayList<>();
in.readList(this.bgColor, Integer.class.getClassLoader());
if (this.bgColor.size() == 0) {
this.bgColor = null;
}
this.borderColor = new ArrayList<>();
in.readList(this.borderColor, Integer.class.getClassLoader());
if (this.borderColor.size() == 0) {
this.borderColor = null;
}
this.borderWidth = in.readInt();
this.borderRadius = new ArrayList<>();
in.readList(this.borderRadius, Integer.class.getClassLoader());
if (this.borderRadius.size() == 0) {
this.borderRadius = null;
}
this.id = in.readString();
this.type = in.readString();
this.l = in.readString();
this.r = in.readString();
this.t = in.readString();
this.b = in.readString();
shadowRadius = in.readInt();
shadowDx = in.readInt();
shadowDy = in.readInt();
this.shadowColor = new ArrayList<>();
in.readList(this.shadowColor, Integer.class.getClassLoader());
if (this.shadowColor.size() == 0) {
this.shadowColor = null;
}
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this.bgColor);
dest.writeList(this.borderColor);
dest.writeInt(this.borderWidth);
dest.writeList(this.borderRadius);
dest.writeString(this.id);
dest.writeString(this.type);
dest.writeString(this.l);
dest.writeString(this.r);
dest.writeString(this.t);
dest.writeString(this.b);
dest.writeInt(shadowRadius);
dest.writeInt(shadowDx);
dest.writeInt(shadowDy);
dest.writeList(shadowColor);
}
public int describeContents() { return 0; }
节点类
public class Node extends UINode {
public static final Creator<Node> CREATOR = new Creator<Node>() {
@Override
public Node createFromParcel(Parcel source) {
return new Node(source);
}
@Override
public Node[] newArray(int size) {
return new Node[size];
}
};
protected Node(Parcel in) {
super(in);
this.children = in.readArray(UINode.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeArray(this.children);
}
}
我正在从Instrumentation.newActivity方法之后的Xposed注入Activity1。
PS:以前,我在从Intent Extras获取模型时遇到了ClassNotFoundException,然后在尝试从Intent获取Extras时设置了类加载器。