当我编写 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 个应用程序,其中一个正在向另一个应用程序发送一个可打包的对象。第一个应用程序可以正常工作以将对象发送给另一个应用程序。BadParcelableException
但是,当第二个应用程序收到它时,它会出错并在我运行该过程时发出。我没有使用 bundle,因为我看到了很多没有必要这样做的例子。如果我错了,请纠正我。