我对parcelable
有疑问。
我的应用程序设计结构如下:
Parcelable
。当我使用意图将数据从一个活动发送到另一个活动或使用捆绑将一个片段发送到另一个片段时,此设计结构很有用。以下是我的POJO类的设计结构:
这是A类
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public class A implements Parcelable {
public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
@Override
public A createFromParcel(Parcel source) {
return new A(source);
}
@Override
public A[] newArray(int size) {
return new A[size];
}
};
private String name;
private String details;
private ArrayList<ParentB> parentBList; //ParentB List
private A(Parcel parcel)
{
this.name = parcel.readString();
this.details = parcel.readString();
this.parentBList = parcel.createTypedArrayList(ParentB.CREATOR);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public ArrayList<ParentB> getParentBList() {
return parentBList;
}
public void setParentBList(ArrayList<ParentB> parentBList) {
this.parentBList = parentBList;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(details);
parcel.writeTypedList(parentBList);
}
}
这是ParentB抽象类:
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public abstract class ParentB implements Parcelable {
public static final Parcelable.Creator<ParentB> CREATOR = new Parcelable.Creator<ParentB>() {
@Override
public ParentB createFromParcel(Parcel source) {
return getClassType(source);
}
@Override
public ParentB[] newArray(int size) {
return new ParentB[size];
}
};
private String className;
private String name;
private String details;
private ArrayList<C> cList; //ParentC List
protected ParentB(Parcel parcel)
{
this.className = parcel.readString();
this.name = parcel.readString();
this.details = parcel.readString();
this.cList = parcel.createTypedArrayList(C.CREATOR);
}
public String getClassName() {
return className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public ArrayList<C> getCList() {
return cList;
}
public void setCList(ArrayList<C> CList) {
this.cList = CList;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(className);
parcel.writeString(name);
parcel.writeString(details);
parcel.writeTypedList(cList);
}
public static ParentB getClassType(Parcel parcel)
{
switch (parcel.readString())
{
case "ChildB1" :
return new ChildB1(parcel);
case "ChildB2" :
return new ChildB2(parcel);
default:
return new ChildB2(parcel);
}
}
}
此ChildB1类是从ParentB扩展的
import android.os.Parcel;
import android.os.Parcelable;
public class ChildB1 extends ParentB
{
public static final Parcelable.Creator<ChildB1> CREATOR = new Parcelable.Creator<ChildB1>() {
@Override
public ChildB1 createFromParcel(Parcel source) {
return new ChildB1(source);
}
@Override
public ChildB1[] newArray(int size) {
return new ChildB1[size];
}
};
private int value;
protected ChildB1(Parcel parcel) {
super(parcel);
this.value = parcel.readInt();
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
super.writeToParcel(parcel, i);
parcel.writeInt(value);
}
}
此ChildB2类是从ParentB扩展的
import android.os.Parcel;
import android.os.Parcelable;
public class ChildB2 extends ParentB
{
public static final Parcelable.Creator<ChildB2> CREATOR = new Parcelable.Creator<ChildB2>() {
@Override
public ChildB2 createFromParcel(Parcel source) {
return new ChildB2(source);
}
@Override
public ChildB2[] newArray(int size) {
return new ChildB2[size];
}
};
public ChildB2(Parcel parcel) {
super(parcel);
}
@Override
public int describeContents() {
return 0;
}
}
这是C类
import android.os.Parcel;
import android.os.Parcelable;
public class C implements Parcelable
{
public static final Parcelable.Creator<C> CREATOR = new Parcelable.Creator<C>() {
@Override
public C createFromParcel(Parcel source) {
return new C(source);
}
@Override
public C[] newArray(int size) {
return new C[size];
}
};
private String name;
private String details;
private D d;
public C(Parcel parcel)
{
this.name = parcel.readString();
this.details = parcel.readString();
this.d = parcel.readParcelable(D.class.getClassLoader()); // can I check here id this class is null or not found? Can I do additional coding here?
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public D getD() {
return d;
}
public void setD(D d) {
this.d = d;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(details);
parcel.writeParcelable(d, i);
}
}
这是D班
import android.os.Parcel;
import android.os.Parcelable;
public class D implements Parcelable {
public static final Parcelable.Creator<D> CREATOR = new Parcelable.Creator<D>() {
@Override
public D createFromParcel(Parcel source) {
return new D(source);
}
@Override
public D[] newArray(int size) {
return new D[size];
}
};
private String name;
private D(Parcel parcel) {
this.name = parcel.readString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
}
}
我正在使用意图将一个对象从活动发送到服务。像聪明的
Intent intent = new Intent(MyActivity.this, MyService.class);
intent.putExtra("A_Object", a); // where a is reference of class A;
startService(intent);
从MyActivity获取对象到MyService时,我使用了以下代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
A a = intent.getParcelableExtra("A_Object");
}
在得到这个A对象时,我在错误之后出现了错误:
E/Parcel: Class not found when unmarshalling:
java.lang.ClassNotFoundException: Invalid name:
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:400)
at android.os.Parcel.readParcelableCreator(Parcel.java:2507)
at android.os.Parcel.readParcelable(Parcel.java:2461)
at com.example.D.<init>(C.java:lineNumber)
我搜索了Class not found when unmarshalling
,但是在所有情况下,我都按照解决方案进行了搜索,这是正确的。同样,仅上述类中的D
类不会发生此问题。
我无法确定为什么会这样?是由于移动设备或应用程序的内存使用情况?
如果是由于内存为空而导致的,那么我可以像在Parcelable
构造函数中那样在C(Parcel)
构造函数中检查该值是否为空。请检查本节中的评论?是一个好选择吗?
在某些问题上,我读到这是在某些移动设备上发生的。因此,如果是由于这个原因,那么我该如何克服这个问题呢?
如果不是因为移动设备,那么在设计代码结构或发送数据时我正在犯什么错误。
请给我适当的反馈和我的问题的解决方案,自上两天以来,我一直陷在这个问题中。