Parcelable Object返回BadParcelableException:当obj = getIntent()时,ClassNotFoundException。getParcelableExtra(" name")

时间:2015-11-17 09:25:12

标签: android classnotfoundexception parcelable badparcelableexception

当我编写pracelable对象并从我编写的另一个应用程序接收时,我遇到了问题。 每次当我试图获取对象时,它总是被证明是BadParcelableException:解组时的ClassNotFoundException。

当我发送到另一个应用程序时,我一直在环顾四周,想知道它是否变坏了。 或者它只是我的代码中有错误。

请告诉我哪里出错了。

我的发送行:

ParcelableObject myObject = new ParcelableObject();
myObject = new ParcelableObject(result_name, resut_lv, result_race, 
    result_job, result_nation, result_guild, result_mission);
intent.putExtra("ACTION_DATA_TRANSFER", myObject);
intent.setClassName(target_app, target_app+".ReceiveMain");
startActivity(intent);

我的接收行:

ParcelableObject myObject = new ParcelableObject();
myObject = getIntent().getParcelableExtra("ACTION_DATA_TRANSFER"); //always crashed at this line

if (getIntent().hasExtra("ACTION_DATA_TRANSFER")) {
    Log.w("****CHECKING****", myObject.getName());
    item1.setText("The Character's name is: " + myObject.getName());
} else {
    Toast.makeText(getApplicationContext(), "There is not data passed yet", 
        Toast.LENGTH_LONG).show();
}

我的ParcelableObject类:

private String name;
private int lv;
private String race;
private String job;
private String nation;
private String guild;
public String mission;

public void setName(String name) {
    this.name = name;
}

public void setLv(int lv) {
    this.lv = lv;
}

public void setRace(String race) {
    this.race = race;
}

public void setJob(String job) {
    this.job = job;
}

public void setNation(String nation) {
    this.nation = nation;
}

public void setGuild(String guild) {
    this.guild = guild;
}

public void setMission(String mission) {
    this.mission = mission;
}

public ParcelableObject(String name, int lv, String race, String job, String
     nation, String guild, String mission) {
    // TODO Auto-generated constructor stub
    this.name = name;
    this.lv = lv;
    this.race = race;
    this.job = job;
    this.nation = nation;
    this.guild = guild;
    this.mission = mission;
}

public String getName() {
    return name;
}

public int getLv() {
    return lv;
}

public String getRace() {
    return race;
}

public String getJob() {
    return job;
}

public String getNation() {
    return nation;
}

public String getGuild() {
    return guild;
}

public String getMission() {
    return mission;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    Log.w("****PO CHECKING****", "describeContents called");
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    Log.w("****PO CHECKING****", "writeToParcel called");
    dest.writeString(name);
    dest.writeString(job);
    dest.writeInt(lv);
    dest.writeString(race);
    dest.writeString(guild);
    dest.writeString(nation);       
    dest.writeString(mission);
}

public ParcelableObject() {
    // TODO Auto-generated constructor stub
    super();
    Log.i("**check if exist**", "start parcel");
}

public static final Parcelable.Creator<ParcelableObject> CREATOR = 
    new Parcelable.Creator<ParcelableObject>() {

    @Override
    public ParcelableObject createFromParcel(Parcel in) {
        // TODO Auto-generated method stub
        Log.w("checking out", "createFromParcel");
        ParcelableObject pObject = new ParcelableObject();
        pObject.name = in.readString();
        pObject.job = in.readString();
        pObject.lv = in.readInt();
        pObject.race = in.readString();
        pObject.guild = in.readString();
        pObject.nation = in.readString();
        pObject.mission = in.readString();

        return pObject;
    }

    @Override
    public ParcelableObject[] newArray(int size) {
        // TODO Auto-generated method stub
        return new ParcelableObject[size];
    }
};

修改 为了澄清一些人认为我是一个懒惰的菜鸟,我一直在寻找答案解决原因,但没有任何运气。我查了很多人的代码并经过多次测试,但仍然无法正确使用我的应用程序。所以这就是我在这里寻求帮助的原因。如果有些人仍然认为我是一个懒惰的菜鸟,我想我太懒了,有些人得出这个结论。我应该重新考虑我的行动。

我的情况是:

我写了2个应用程序,其中一个正在向另一个应用程序发送一个parcelable对象。第一个应用程序,它正确地将对象发送到另一个。但是,当第二个应用程序收到它时,它会出错并在我运行该过程时发出BadParcelableException。我没有使用bundle,因为我看到很多例子没有必要这样做。如果我错了,请纠正我。

2 个答案:

答案 0 :(得分:0)

我找到了与此主题相关的答案...... Is using Parcelable the right way to send data between applications?

感谢大家的上述评论......

答案 1 :(得分:0)

我使用serializable代替Parcelable来解决我的问题。

但是在这种情况下,尝试解析像这样的对象

myObject = (ParcelableObject)getIntent().getParcelableExtra("ACTION_DATA_TRANSFER");