我有一个名为AdViewed
的类,它实现了Parcelable
。
以下是代码:
public class AdViewed implements Parcelable {
protected long timeStamp;
protected String country, city, lat, lng, ad_id, poster_id, user_id, latLng;
public void hydrate(HashMap<String, ?> map){
Method[] methodes = this.getClass().getMethods();
Iterator it = map.keySet().iterator();
while(it.hasNext()){
Object key = it.next();
Object value = map.get(key);
String valueS = value.toString();
String method = "set" + key.toString();
for(Method methode : methodes){
if(methode.getName().equals(method)){
try{
methode.invoke(AdViewed.this, valueS);
}
catch(InvocationTargetException e){
e.printStackTrace();
}
catch(IllegalAccessException e){
e.printStackTrace();
}
}
}
}
}
public AdViewed(long timeStamp1, String country1, String city1, String lat1, String lng1, String ad_id1, String poster_id1,
String user_id1, String latLng1){
this.timeStamp = timeStamp1;
this.country = country1;
this.city = city1;
this.lat = lat1;
this.lng = lng1;
this.ad_id = ad_id1;
this.poster_id = poster_id1;
this.user_id = user_id1;
this.latLng = latLng1;
}
public AdViewed(){
this.timeStamp = 0 ;
this.country = "";
this.city = "";
this.lat = "";
this.lng = "";
this.ad_id = "";
this.poster_id = "";
this.user_id = "";
this.latLng = "";
}
public AdViewed(HashMap<String, ?> map){
this.timeStamp = 0;
this.country = "";
this.city = "";
this.lat = "";
this.lng = "";
this.ad_id = "";
this.poster_id = "";
this.user_id = "";
this.latLng = "";
if(!map.isEmpty()){
this.hydrate(map);
//erreurs = new ArrayList();
}
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
dest.writeLong(timeStamp);
dest.writeString(country);
dest.writeString(city);
dest.writeString(lat);
dest.writeString(lng);
dest.writeString(ad_id);
dest.writeString(poster_id);
dest.writeString(user_id);
dest.writeString(latLng);
}
public static final Parcelable.Creator<AdViewed> CREATOR = new Parcelable.Creator<AdViewed>(){
@Override
public AdViewed createFromParcel(Parcel source) {
return new AdViewed(source);
}
@Override
public AdViewed[] newArray(int size) {
return new AdViewed[size];
}
};
public AdViewed(Parcel in){
timeStamp = in.readLong();
country = in.readString();
city = in.readString();
lat = in.readString();
lng = in.readString();
ad_id = in.readString();
poster_id = in.readString();
user_id = in.readString();
latLng = in.readString();
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
public String getAd_id() {
return ad_id;
}
public void setAd_id(String ad_id) {
this.ad_id = ad_id;
}
public String getPoster_id() {
return poster_id;
}
public void setPoster_id(String poster_id) {
this.poster_id = poster_id;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getLatLng() {
return latLng;
}
public void setLatLng(String latLng) {
this.latLng = latLng;
}
}
我还有另一个来自AdViewed的课程AdInDeal
。这是它的代码:
public class AdInDeal extends AdViewed {
protected String qte, costPerUnit, totalCost, imgPath;
protected boolean onCommand, onAcheteur, onVendeur;
protected int statutDeal;
public AdInDeal(AdInDeal adInDeal){
myInflation(adInDeal);
}
public void myInflation(AdInDeal adInDeal1){
this.setUser_id(adInDeal1.user_id);
this.setCostPerUnit(adInDeal1.getCostPerUnit());
this.setImgPath(adInDeal1.getImgPath());
this.setOnAcheteur(adInDeal1.isOnAcheteur());
this.setOnCommand(adInDeal1.isOnCommand());
this.setOnVendeur(adInDeal1.isOnVendeur());
this.setQte(adInDeal1.getQte());
this.setStatutDeal(adInDeal1.getStatutDeal());
this.setTotalCost(adInDeal1.getTotalCost());
this.setAd_id(adInDeal1.getAd_id());
this.setCity(adInDeal1.getCity());
this.setCountry(adInDeal1.getCountry());
this.setLat(adInDeal1.getLat());
this.setLng(adInDeal1.getLng());
this.setLatLng(adInDeal1.getLatLng());
this.setPoster_id(adInDeal1.getPoster_id());
this.setTimeStamp(adInDeal1.getTimeStamp());
}
public AdInDeal(long timeStamp1, String country1, String city1, String lat1, String lng1, String ad_id1, String poster_id1,
String user_id1, String latLng1, String qte1, String costPerUnit1, String totalCost1){
super(timeStamp1, country1, city1, lat1, lng1, ad_id1, poster_id1,
user_id1, latLng1);
this.qte = qte1;
this.costPerUnit = costPerUnit1;
this.totalCost = totalCost1;
this.imgPath = "";
this.onCommand = false;
this.onAcheteur = false;
this.onVendeur = false;
this.statutDeal = 0;
}
public AdInDeal(long timeStamp1, String country1, String city1, String lat1, String lng1, String ad_id1, String poster_id1,
String user_id1, String latLng1, String qte1, String costPerUnit1, String totalCost1, String imgPath1){
super(timeStamp1, country1, city1, lat1, lng1, ad_id1, poster_id1,
user_id1, latLng1);
this.qte = qte1;
this.costPerUnit = costPerUnit1;
this.totalCost = totalCost1;
this.imgPath = imgPath1;
this.onCommand = false;
this.onAcheteur = false;
this.onVendeur = false;
this.statutDeal = 0;
}
public AdInDeal(long timeStamp1, String country1, String city1, String lat1, String lng1, String ad_id1, String poster_id1,
String user_id1, String latLng1, String qte1, String costPerUnit1, String totalCost1,
String imgPath1, boolean onCommand1, boolean onAcheteur1, boolean onVendeur1, int statutDeal1){
super(timeStamp1, country1, city1, lat1, lng1, ad_id1, poster_id1,
user_id1, latLng1);
this.qte = qte1;
this.costPerUnit = costPerUnit1;
this.totalCost = totalCost1;
this.imgPath = imgPath1;
this.onCommand = onCommand1;
this.onAcheteur = onAcheteur1;
this.onVendeur = onVendeur1;
this.statutDeal = statutDeal1;
}
public AdInDeal(){
super();
this.qte = "";
this.costPerUnit = "";
this.totalCost = "";
this.imgPath = "";
this.onCommand = false;
this.onAcheteur = false;
this.onVendeur = false;
this.statutDeal = 0;
}
public AdInDeal(HashMap<String, ?> map){
super(map);
this.qte = "";
this.costPerUnit = "";
this.totalCost = "";
this.imgPath = "";
this.onCommand = false;
this.onAcheteur = false;
this.onVendeur = false;
this.statutDeal = 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
super.writeToParcel(dest, flags);
dest.writeString(qte);
dest.writeString(costPerUnit);
dest.writeString(totalCost);
dest.writeString(imgPath);
dest.writeInt(statutDeal);
dest.writeByte((byte) (onCommand ? 1 : 0));
dest.writeByte((byte) (onAcheteur ? 1 : 0));
dest.writeByte((byte) (onVendeur ? 1 : 0));
}
public AdInDeal(Parcel in){
super(in);
qte = in.readString();
costPerUnit = in.readString();
totalCost = in.readString();
imgPath = in.readString();
statutDeal = in.readInt();
onCommand = in.readByte() != 0;
onAcheteur = in.readByte() != 0;
onVendeur = in.readByte() != 0;
}
public String getQte() {
return qte;
}
public void setQte(String qte) {
this.qte = qte;
}
public String getCostPerUnit() {
return costPerUnit;
}
public void setCostPerUnit(String costPerUnit) {
this.costPerUnit = costPerUnit;
}
public String getTotalCost() {
return totalCost;
}
public void setTotalCost(String totalCost) {
this.totalCost = totalCost;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public boolean isOnCommand() {
return onCommand;
}
public void setOnCommand(boolean onCommand) {
this.onCommand = onCommand;
}
public boolean isOnVendeur() {
return onVendeur;
}
public void setOnVendeur(boolean onVendeur) {
this.onVendeur = onVendeur;
}
public boolean isOnAcheteur() {
return onAcheteur;
}
public void setOnAcheteur(boolean onAcheteur) {
this.onAcheteur = onAcheteur;
}
public int getStatutDeal() {
return statutDeal;
}
public void setStatutDeal(int statutDeal) {
this.statutDeal = statutDeal;
}
}
我还有Activity
,我们称之为ActivityA
。 ActivityA
使用Intent
转到其他活动(我们称之为ActivityB
)。
以下是ActivityA
的代码:
AdInDeal adInDeal = new AdInDeal(111111111, "Canada", "Torronto", "43.620595", "-79.376129", "id1", "poster_id1" , "user_id1", "43.620595, -79.376129", "10", "100", "1000", "imgPath1")
Intent intent = new Intent(activityA.this, activityB.class);
intent.putExtra("text1", "A small text");
intent.putExtra("text2", adInDeal);//If I delete this line I have no problem
startActivity(intent);
以下是ActivityB
的代码:
Intent mIntent = getIntent();
if(mIntent != null){
String mText = mIntent.getStringExtra("text1");
AdInDeal mAdInDeal = mIntent.getParcelableExtra("text2");
Toast.makeText(this, mAdInDeal.getQte(), Toast.LENGTH_LONG).show();
}
当我编译并运行此代码时,我得到一个IllegalArgumentException
。
根据调试器的不同,它来自以下行:
AdInDeal mAdInDeal = mIntent.getParcelableExtra("text2");
在ActivityB
。
但是我发现问题实际上在ActivityA
的行中:
intent.putExtra("text2", adInDeal);
为什么会这样?