我尝试传递" Set"从主活动到在不同进程中运行的后台服务的值(反之亦然)。因为我还没有找到任何" Bundle"也不是" Message.Obj"方法处理"设置",我转换" Set"进入" ArrayList"。使用此源发送消息" ArrayList"似乎没有错误发生,因为服务获取消息。但是,当我尝试检索" ArrayList"来自"消息",出现错误。
以下是" Parcelable"的源代码。类:
public class SavedCell implements Parcelable {
public String cssId;
public boolean isFinalized;
public boolean isInduced;
public Set<Integer> potential;
public SavedCell(Cell c) {
this.cssId = c.cssId;
this.isFinalized = c.isFinalized;
this.isInduced = c.isInduced;
this.potential = new HashSet(c.potential);
}
public SavedCell(String id, boolean f, boolean i, Set<Integer> p) {
this.cssId = id;
this.isFinalized = f;
this.isInduced = i;
this.potential = new HashSet(p);
}
public SavedCell(Parcel p) {
this.cssId = p.readString();
this.isFinalized = (p.readByte() != 0);
this.isInduced = (p.readByte() != 0);
int[] potentialArray = p.createIntArray();
this.potential = new HashSet();
for (int i : potentialArray) {
this.potential.add(i);
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.cssId);
dest.writeByte((byte) (this.isFinalized ? 1 : 0));
dest.writeByte((byte) (this.isInduced ? 1 : 0));
int[] potentialArray = new int[this.potential.size()];
int idx = 0;
for (Iterator it=this.potential.iterator(); it.hasNext();) {
potentialArray[idx++] = (int) it.next();
}
dest.writeIntArray(potentialArray);
}
public static final Creator<SavedCell> CREATOR = new Creator<SavedCell>() {
@Override
public SavedCell createFromParcel(Parcel in) {
return new SavedCell(in);
}
@Override
public SavedCell[] newArray(int size) {
return new SavedCell[size];
}
};
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final SavedCell other = (SavedCell) obj;
return this.cssId.equalsIgnoreCase(other.cssId);
}
}
以下是处理主Activity中数据发送的代码:
public void createInitialSnapshot(Set<SavedCell> sgc, boolean initialisePuzzle) {
ArrayList<SavedCell> bundledPuzzle = new ArrayList<>(sgc);
if (isSolveServiceBound) {
try {
Bundle b = new Bundle();
b.putParcelableArrayList("bundledPuzzle", bundledPuzzle);
Message msg = Message.obtain(null, MSG_SAVE_INITIAL_SNAPSHOT, (initialisePuzzle) ? 1 : 0, 0, null);
msg.setData(b);
msg.replyTo = messengerSolveClient;
messengerSolveService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
最后,代码尝试在后台服务进程中检索数据:
private void saveInitialSnapShot(Message msg) {
Bundle b = msg.getData();
if (b == null) {
...
} else {
...
ArrayList<SavedCell> bundledPuzzle = b.getParcelableArrayList("bundledPuzzle");
Set<SavedCell> sgc = new HashSet(bundledPuzzle);
...
}
围绕&#34; ArrayList bundledPuzzle = b.getParcelableArrayList(&#34; bundledPuzzle&#34;);&#34;在try-catch块中的语句,报告的异常是&#34; android.os.BadParcelableException:解组时的ClassNotFoundException:be.ema.sc.SavedCell&#34;。