【问题标题】:I am getting java.lang.runtimeexception parcelable encountered ioexception writing serializable object我得到 java.lang.runtimeexception parcelable 遇到 ioexception 写入可序列化对象
【发布时间】:2016-10-17 10:52:30
【问题描述】:

我是 android 的新手,正在开发一个带有两个屏幕的演示应用程序,将数据从一个屏幕传递到另一个活动,在此期间我遇到了异常,

java.lang.RuntimeException: Parcelable遇到IOException写 可序列化对象(名称 = one.tusk.stush.connect.Post)

代码

  Intent intentPostDetail = new Intent(PostListItem.this.getContext(), NewPostDetailActivity.class);
                Post post = mPost;
                System.out.print("========MY POST IS======>" + mPost.toString());
                intentPostDetail.putExtra("Post", post);
                intentPostDetail.putExtra("flag", "post");
            mContext.startActivity(intentPostDetail);

基础对象

public class BaseObject {

    static DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

    public static Date getDateFromJSONObject(JSONObject jsonObject, String key) {

        String value = "";
        Date date = null;
        try {
            value = jsonObject.getString(key);
            date = dateFormatter.parse(value);
        } catch (Exception e) {
        }
        return date;

    }
    public static String getStringFromJSONObject(JSONObject jsonObject, String key) {

        String value = "";
        try {
            value = jsonObject.getString(key);
        } catch (Exception e) {
        }
        return value;
    }

    public static int getIntFromJSONObject(JSONObject jsonObject, String key) {

        int value = 0;
        try {
            value = jsonObject.getInt(key);
        } catch (Exception e) {
        }
        return value;
    }

    public static boolean getBooleanFromJSONObject(JSONObject jsonObject, String key) {

        boolean value = false;
        try {
            value = jsonObject.getBoolean(key);
        } catch (Exception e) {
        }
        return value;
    }

    public static double getDoubleFromJSONObject(JSONObject jsonObject, String key) {

        double value = 0;
        try {
            value = jsonObject.getDouble(key);
        } catch (Exception e) {
        }
        return value;
    }



    public static JSONObject getJSONFromJSONObject(JSONObject jsonObject, String key) {

        JSONObject value = null;
        try {
            value = jsonObject.getJSONObject(key);
        } catch (Exception e) {
        }
        return value;
    }

发帖

public class Post extends BaseObject implements Serializable {

    private static final long serialVersionUID = 1L;
    public String postTitle;
    public String postImagePath;
    public Date postDate;
    public ArrayList<String> postKeywords;
    public User postUser;
    public int postID;
    public int postLikesCount;
    public int postCommentsCount;
    public boolean likedThisPost;
    public boolean commentedThisPost;
    public boolean inAlbum;
    public String timeAgo;
    public int totalReviews;
    public String loginuserReviews;
    int rv_cnt;
    public double ratingcount;
    public JSONObject userObj;
    public int userId;

    String reviews;

    public Post(JSONObject jsonObject) {
        //Log.d("JSOn", jsonObject.toString());
        this.postID = getIntFromJSONObject(jsonObject, "postID");
        this.postImagePath = getStringFromJSONObject(jsonObject, "postImage");
        this.postTitle = getStringFromJSONObject(jsonObject, "postTitle");
        this.postDate = getDateFromJSONObject(jsonObject, "postDate");
        this.postUser = new User(getJSONFromJSONObject(jsonObject, "user"));
        this.postLikesCount = getIntFromJSONObject(jsonObject, "totalLikes");
        this.postCommentsCount = getIntFromJSONObject(jsonObject, "totalComments");
        this.timeAgo = getStringFromJSONObject(jsonObject, "timeAgo");
        this.commentedThisPost = getBooleanFromJSONObject(jsonObject, "isCommented");
//      userObj = getJSONFromJSONObject(jsonObject, "user");
        this.userId = getIntFromJSONObject(jsonObject, "userID");
        this.totalReviews = getIntFromJSONObject(jsonObject, "totalReview");
        this.ratingcount = getDoubleFromJSONObject(jsonObject, "ratingcount");


        int isLiked = getIntFromJSONObject(jsonObject, "isLiked");
        if (isLiked == 1) {
            this.likedThisPost = true;
        } else {
            this.likedThisPost = false;
        }

        int inAlbum = getIntFromJSONObject(jsonObject, "inAlbum");
        if (inAlbum == 1) {
            this.inAlbum = true;
        } else {
            this.inAlbum = false;
        }
    }

}

谁能帮我解决这个问题。

【问题讨论】:

  • 在问题中输入Post class。
  • 什么是Post类?
  • @Ironman - 哪个班级?
  • 好的,我正在发布“发布”对象类
  • @Apurva - 是的,我已经发布了我的帖子对象类..请帮助我

标签: android android-intent parcelable serializable


【解决方案1】:

在模型类 Post 上实现 Serializable。这将修复错误。

   public class Post implements Serializable {

【讨论】:

  • 你的BaseObject 课程是什么??
  • 我已在我的问题中发布,请参阅
  • 您好,我找到了原因,当我添加了`public static JSONObject getJSONFromJSONObject(JSONObject jsonObject, String key) { JSONObject value = null;尝试 { value = jsonObject.getJSONObject(key); } catch (Exception e) { } 返回值; }` 在我的基础对象中我发现了这个异常,
【解决方案2】:

让你的 BaseObject 类也像这样实现 Serializable

public class BaseObject implements Serializable
{
}

如果仍然存在问题,请使用 BaseObject 类更新您的问题。

【讨论】:

  • 嘿,我已经发布了我的基本对象 ot 我的问题..请看,请帮帮我
  • 您好,我找到了原因,当我添加了`public static JSONObject getJSONFromJSONObject(JSONObject jsonObject, String key) { JSONObject value = null;尝试 { value = jsonObject.getJSONObject(key); } catch (Exception e) { } 返回值; }` 在我的基础对象中我发现了这个异常,
猜你喜欢
  • 2016-01-25
  • 2019-04-26
  • 2021-12-04
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2014-06-02
相关资源
最近更新 更多