有兩個類,彼此之間相互鏈接。問題是有異常#1,如果我實現Parceable這樣:Parcelable cause Stackoverflow
RSSFeed.java
public class RSSFeed implements Parcelable {
private String title;
private String link;
private ArrayList<RSSItem> rssItems;
public RSSFeed() {
rssItems = new ArrayList<RSSItem>();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.title, this.link });
Bundle b = new Bundle();
b.putParcelableArrayList("rssItems", rssItems);
dest.writeBundle(b);
}
public static final Parcelable.Creator<RSSFeed> CREATOR = new Parcelable.Creator<RSSFeed>() {
@Override
public RSSFeed createFromParcel(Parcel in) {
RSSFeed rssFeed = new RSSFeed();
String[] data = new String[2];
in.readStringArray(data);
rssFeed.title = data[0];
rssFeed.link = data[1];
Bundle b = in.readBundle(RSSItem.class.getClassLoader());
rssFeed.rssItems = b.getParcelableArrayList("rssItems");
return rssFeed;
}
@Override
public RSSFeed[] newArray(int size) {
return new RSSFeed[size];
}
};
RSSItem.java
public class RSSItem implements Parcelable {
private RSSFeed feed;
private String title;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle b = new Bundle();
b.putParcelable("feed", this.feed);
dest.writeStringArray(new String[] { this.title });
}
public static final Parcelable.Creator<RSSItem> CREATOR = new Parcelable.Creator<RSSItem>() {
@Override
public RSSItem createFromParcel(Parcel in) {
RSSItem rssItem = new RSSItem();
Bundle b = in.readBundle(RSSFeed.class.getClassLoader());
rssItem.feed = b.getParcelable("feed");
String[] dataString = new String[1];
in.readStringArray(dataString);
rssItem.setTitle(dataString[0]);
return rssItem;
}
@Override
public RSSItem[] newArray(int size) {
return new RSSItem[size];
}
};
所以,它擠壓我stackoverflow我猜,因爲它試圖在writeParcel(...)
通過ArrayList<RSSItems>
哪一個在writeParcel(...)
試圖通過RSSFeed
成員和一次又一次。那麼,問題是如何解決這個問題Parce兩個類?
和確切的堆棧跟蹤是? –