Android app unable to start activity componentinfo

1.我的错误位置如图

public void onClick(View v) {
    Intent intent = new Intent(mContext, GoodsDetailActivity.class);
    intent.putExtra("goodsModel", model);
    mContext.startActivity(intent);
}

2.无法放入model(model是一个实体类的对象) 

在这里提及一下:Java的序列化机制

Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。添加serialVersionUID使得在序列化时保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。

3.//我一开始采用实现Seriazable接口不好使(可能因为我没搞懂),然后使用 了Parcelable

由于在传递对象时对于需要传递的对象的序列化选择可以加以区分,需要数据持久化的建议实现Serializable接口,只在内存间数据传输时推荐使用Parcelable。

 4.我通过实现了Parcelable接口就好使了(你的实体实现Parcelable接口)

当你实现的Parcelable接口会出现下列如下(请根据自己的代码进行比对)

import android.os.Parcel;
import android.os.Parcelable;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;



@Entity
public class GoodsModel  implements Parcelable {

    private static final Long serialVersionUID=1L;

    @Id(autoincrement = true)
    private Long id;
    private Integer goodsId;
    private String name;
    private String icon;
    private String info;
    private String type;
    @Generated(hash = 1834473137)
    public GoodsModel(Long id, Integer goodsId, String name, String icon,
                      String info, String type) {
        this.id = id;
        this.goodsId = goodsId;
        this.name = name;
        this.icon = icon;
        this.info = info;
        this.type = type;
    }
    @Generated(hash = 971639536)
    public GoodsModel() {
    }

    protected GoodsModel(Parcel in) {
        if (in.readByte() == 0) {
            id = null;
        } else {
            id = in.readLong();
        }
        if (in.readByte() == 0) {
            goodsId = null;
        } else {
            goodsId = in.readInt();
        }
        name = in.readString();
        icon = in.readString();
        info = in.readString();
        type = in.readString();
    }

    public static final Creator<GoodsModel> CREATOR = new Creator<GoodsModel>() {
        @Override
        public GoodsModel createFromParcel(Parcel in) {
            return new GoodsModel(in);
        }

        @Override
        public GoodsModel[] newArray(int size) {
            return new GoodsModel[size];
        }
    };

    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Integer getGoodsId() {
        return this.goodsId;
    }
    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getIcon() {
        return this.icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }
    public String getInfo() {
        return this.info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    public String getType() {
        return this.type;
    }
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (id == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(id);
        }
        if (goodsId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(goodsId);
        }
        dest.writeString(name);
        dest.writeString(icon);
        dest.writeString(info);
        dest.writeString(type);
    }
}

我们可以看到在方法中有很多个Parcel对象。这个类是用来封装数据的容器,封装后的数据可以通过Intent或IPC传递,除了基本类型外,只有实现了Parcelable接口的类才能放入parcel中,也就是说实现了Parcelable接口后可以把数据打包成Parcel对象对象来传递,并且只有实现了Parcelable接口的类才能放入parcel中,那实现了Parcelable接口的对象是如何打包和读取的呢?我们会发现两个方法

  • public void writeToParcel(Parcel dest, int flags)
    这个方法就是传入一个空的Parcel对象,然后将我们要存储的对象的属性写进Parcel中去(注意写入顺序,因为在读取是时要按照写入顺序来读取)
public void writeToParcel(Parcel dest, int flags) {
    if (id == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(id);
    }
    if (goodsId == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeInt(goodsId);
    }
    dest.writeString(name);
    dest.writeString(icon);
    dest.writeString(info);
    dest.writeString(type);
}

 还有一个就是:

-public GoodsModel createFromParcel(Parcel in)

从方法名我们就可以看出他是从Parcel对象中获取值来创建一个我们需要传递的的GoodsModel对象

@Override
public GoodsModel createFromParcel(Parcel in) {
    return new GoodsModel(in);
}

它调用了GoodsModel的构造方法

 public GoodsModel(Long id, Integer goodsId, String name, String icon, String info, String type) { this.id = id; this.goodsId = goodsId; this.name = name; this.icon = icon; this.info = info; this.type = type; } 

也就是说对于对象传递时需要打包成Parcel对象传递,而打包和解包的过程都是我们自己来写的。

题外话:对于实现Parcelable接口实现方法十分容易写错,如果你用android studio的话,安装个*Android
Parcelable code generator*插件在你要实现Parcelable接口的bean上按住
alt+insert选择Parcelable,就可以快速生成Parcelable代码

参考: https://blog.csdn.net/daydayplayphone/article/details/52579577

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幽香飞狐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值