2016-03-02 78 views
-1

我正在研究Android應用程序,並且我想通過意向將一個Cutom列表對象傳遞給其他活動。 我已經使該CustomList類實現Serializable接口。 並使用下面的代碼通過發送意圖的數據:如何使用意圖傳遞自定義列表對象

FeedList mSelectedFeedList = mFeedItemList.get(position); 
Intent intent = new Intent(mContext, SingleFeedPage.class);           intent.putExtra("feedItemPosition", listItemHolder.mSelectedPosition); 
intent.putExtra("action_id", mSelectedFeedList.getmActionId()); 
intent.putExtra("feedList", mSelectedFeedList); 

當我通過intent我提示以下錯誤:發送此mSelectedFeedList:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.socialengineaddons.mobileapp.classes.common.utils.FeedList) 

下面是我的代碼FeedList.java

public class FeedList { 

private String mFeedIcon, mFeedTitle, mFeedPostTime, mWebUrl, mActionTypeBody, mFeedAttachmentType; 
private int mAttachmentCount, mLikeCount, mCommentCount, mIsLike, mPhotoAttachmentCount; 
private int mCommentAble, mShareAble; 
private int mCanComment; 
private JSONArray mFeedMenusArray, mFeedAttachmentArray, mFeedFilterArray; 
private JSONObject mFeedFooterMenus, mFeedPostMenus,mFeedObject; 
String mHiddenBodyText, mUndoHiddenFeedURl, mHideAllText, mHideAllUrl, mHideAllName, mFeedType; 
int mSubjectId, mActionId; 
Map<String, String> mClickableStringsList; 
Map<Integer, String> mVideoInfo; 
boolean noFeed; 
ArrayList<PhotoListDetails> mPhotoDetails; 

public FeedList(int actionId, int subjectId, String mFeedTitle, String mFeedIcon, JSONArray 
     mFeedMenusArray, String mFeedPostTime, int attachmentCount, int likeCount, int commentCount, 
       int canComment, int isLike, JSONObject feedObject, JSONArray feedAttachmentArray, int photoAttachmentCount, 
       JSONObject feedFooterMenus, int commentAble, int shareAble, 
       Map<String, String> clickableStrings, String actionTypeBody, Map<Integer, String> videoInfo, 
       String url, String feedAttachmentType, String type) { 

    this.mSubjectId = subjectId; 

    this.mFeedTitle = mFeedTitle; 
    this.mFeedIcon = mFeedIcon; 
    this.mFeedMenusArray = mFeedMenusArray; 
    this.mFeedPostTime = mFeedPostTime; 

    mAttachmentCount = attachmentCount; 
    mLikeCount = likeCount; 
    mCommentCount = commentCount; 
    mIsLike = isLike; 
    mCanComment = canComment; 

    this.mFeedObject = feedObject; 
    this.mFeedAttachmentArray = feedAttachmentArray; 
    this.mPhotoAttachmentCount = photoAttachmentCount; 

    this.mCommentAble = commentAble; 
    this.mShareAble = shareAble; 

    mFeedFooterMenus = feedFooterMenus; 

    mActionId = actionId; 

    mClickableStringsList = clickableStrings; 
    mVideoInfo = videoInfo; 

    mWebUrl = url; 
    mActionTypeBody = actionTypeBody; 

    mFeedAttachmentType = feedAttachmentType; 
    mFeedType = type; 

} 

public FeedList(JSONObject feedPostMenus, JSONArray FeedFilterArray, boolean isNoFeed){ 
    mFeedPostMenus = feedPostMenus; 
    mFeedFilterArray = FeedFilterArray; 
    this.noFeed = isNoFeed; 
} 

public boolean isNoFeed() { 
    return noFeed; 
} 

public String getmFeedIcon() { 
    return mFeedIcon; 
} 

public String getmFeedTitle() { 
    return mFeedTitle; 
} 

public String getmFeedPostTime() { 
    return mFeedPostTime; 
} 

public JSONArray getmFeedMenusArray() { 
    return mFeedMenusArray; 
} 

public void setmFeedMenusArray(JSONArray mFeedMenusArray) { 
    this.mFeedMenusArray = mFeedMenusArray; 
} 

public int getmAttachmentCount() { 
    return mAttachmentCount; 
} 

public int getmLikeCount() { 
    return mLikeCount; 
} 

public void setmLikeCount(int mLikeCount) { 
    this.mLikeCount = mLikeCount; 
} 

public int getmCommentCount() { 
    return mCommentCount; 
} 

public void setmCommentCount(int mCommentCount) { 
    this.mCommentCount = mCommentCount; 
} 

public int getmIsLike() { 
    return mIsLike; 
} 

public void setmIsLike(int mIsLike) { 
    this.mIsLike = mIsLike; 
} 

public int ismCanComment() { 
    return mCanComment; 
} 

public int getmPhotoAttachmentCount() { 
    return mPhotoAttachmentCount; 
} 

public JSONArray getmFeedAttachmentArray() { 
    return mFeedAttachmentArray; 
} 

public JSONObject getmFeedFooterMenus() { 
    return mFeedFooterMenus; 
} 

public void setmFeedFooterMenus(JSONObject mFeedFooterMenus) { 
    this.mFeedFooterMenus = mFeedFooterMenus; 
} 

public String getmHiddenBodyText() { 
    return mHiddenBodyText; 
} 

public void setmHiddenBodyText(String mHiddenBodyText) { 
    this.mHiddenBodyText = mHiddenBodyText; 
} 

public String getmUndoHiddenFeedURl() { 
    return mUndoHiddenFeedURl; 
} 

public void setmUndoHiddenFeedURl(String mUndoHiddenFeedURl) { 
    this.mUndoHiddenFeedURl = mUndoHiddenFeedURl; 
} 

public String getmHideAllText() { 
    return mHideAllText; 
} 

public void setmHideAllText(String mHideAllText) { 
    this.mHideAllText = mHideAllText; 
} 

public String getmHideAllUrl() { 
    return mHideAllUrl; 
} 

public void setmHideAllUrl(String mHideAllUrl) { 
    this.mHideAllUrl = mHideAllUrl; 
} 

public String getmHideAllName() { 
    return mHideAllName; 
} 

public void setmHideAllName(String mHideAllName) { 
    this.mHideAllName = mHideAllName; 
} 

public int getmCommentAble() { 
    return mCommentAble; 
} 

public void setmCommentAble(int mCommentAble) { 
    this.mCommentAble = mCommentAble; 
} 

public int getmShareAble() { 
    return mShareAble; 
} 

public void setmShareAble(int mShareAble) { 
    this.mShareAble = mShareAble; 
} 

public int getmSubjectId() { 
    return mSubjectId; 
} 

public void setmSubjectId(int mSubjectId) { 
    this.mSubjectId = mSubjectId; 
} 

public int getmActionId() { 
    return mActionId; 
} 

public Map<String, String> getmClickableStringsList() { 
    return mClickableStringsList; 
} 


public JSONArray getmFeedFilterArray() { 
    return mFeedFilterArray; 
} 


public JSONObject getmFeedPostMenus() { 
    return mFeedPostMenus; 
} 


public ArrayList<PhotoListDetails> getmPhotoDetails() { 
    return mPhotoDetails; 
} 

public void setmPhotoDetails(ArrayList<PhotoListDetails> mPhotoDetails) { 
    this.mPhotoDetails = mPhotoDetails; 
} 

public String getmActionTypeBody() { 
    return mActionTypeBody; 
} 

public void setmActionTypeBody(String mActionTypeBody) { 
    this.mActionTypeBody = mActionTypeBody; 
} 

public JSONObject getmFeedObject() { 
    return mFeedObject; 
} 

public void setmFeedObject(JSONObject mFeedObject) { 
    this.mFeedObject = mFeedObject; 
} 

public Map<Integer, String> getmVideoInfo() { 
    return mVideoInfo; 


} 

public String getmWebUrl() { 
    return mWebUrl; 
} 

public String getmFeedType() { 
    return mFeedType; 
} 

public String getmFeedAttachmentType() { 
    return mFeedAttachmentType; 
} 

}

我怎樣才能解決這個問題,一個對此有任何想法。

非常感謝高級!!!

+0

PL份額完整的堆棧跟蹤 –

+0

也有着FeedList類.. – Sachin

+0

考慮實現Parcelable代替Serializable接口,見[此答案(http://stackoverflow.com/a/23647471/5015207) – 0X0nosugar

回答

2

您必須使用parcelable類。

public class Student implements Parcelable{ 
     private String id; 
     private String name; 


     // Constructor 
     public Student(String id, String name){ 
      this.id = id; 
      this.name = name; 

     } 
     // Getter and setter methods 
     ......... 
     ......... 

     // Parcelling part 
     public Student(Parcel in){ 
      String[] data = new String[3]; 

      in.readStringArray(data); 
      this.id = data[0]; 
      this.name = data[1]; 

     } 

     @Оverride 
     public int describeContents(){ 
      return 0; 
     } 

     @Override 
     public void writeToParcel(Parcel dest, int flags) { 
      dest.writeStringArray(new String[] {this.id, 
               this.name }); 
     } 
     public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
      public Student createFromParcel(Parcel in) { 
       return new Student(in); 
      } 

      public Student[] newArray(int size) { 
       return new Student[size]; 
      } 
     }; 
    } 

使用:

intent.putExtra("student", new Student("1","Mike")); 

獲取數據:

Bundle data = getIntent().getExtras(); 
Student student = (Student) data.getParcelable("student"); 

或者你可以使用這個庫:

https://github.com/johncarl81/parceler

或者使用序列化:

public class Student implements Serializable { 
     private String id; 
     private String name; 



     public Student(String id, String name){ 
      this.id = id; 
      this.name = name; 

     } 
     public string getId() { 
      id; 
     } 


     public string setName() { 
     name; 
     } 

    } 

嘗試通過可序列化;

Bundle bundle = new Bundle(); 
bundle.putSerializable("student", new Student("1","Mike")); 
intent.putExtras(bundle); 

得到:

Intent intent = this.getIntent(); 
Bundle bundle = intent.getExtras(); 

Student st=(Student)bundle.getSerializable("value"); 
1

讓您FeedListParcelable類。使用Parcelabler工具創建Parcelable類。

然後把意圖這樣的 -

intent.putExtra("feedList", mSelectedFeedList); 

收到像這個 -

Bundle data = getIntent().getExtras(); 
Feedlist feedlist= (FeedList) data.getParcelable("feedList"); 
相關問題