2016-12-26 151 views
0

我正在嘗試使用parcelable將對象數組從一個活動傳遞給另一個活動。在這裏,我遇到這個問題解組時找不到課程:解組時未找到類:Parcelable(Android)

第一個活動代碼

intent.setExtrasClassLoader(MenueItemDetails.class.getClassLoader()); 
    intent.putExtra("menue",myArray); 

次活動代碼

myArray = (MenueItemDetails[])getIntent().getParcelableArrayExtra("menue"); 

這是我的類,這是parceable

public class MenueItemDetails implements Parcelable { 

private int id = 0, menueId = 0, type = 0, typeId = 0, styleId = 0, lineBefore = 0; 
private String webSite = "", title = "", icon = ""; 


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

// write your object's data to the passed-in Parcel 
@Override 
public void writeToParcel(Parcel out, int flags) { 
    out.writeInt(id); 
    out.writeInt(menueId); 
    out.writeInt(type); 
    out.writeInt(typeId); 
    out.writeInt(styleId); 
    out.writeInt(lineBefore); 
    out.writeString(webSite); 
    out.writeString(title); 
    out.writeString(icon); 
} 
private MenueItemDetails(Parcel in) { 
    id = in.readInt(); 
    menueId = in.readInt(); 
    type = in.readInt(); 
    typeId = in.readInt(); 
    styleId= in.readInt(); 
    lineBefore= in.readInt(); 
    webSite=in.readString(); 
    title= in.readString(); 
    icon=in.readString(); 
} 
public MenueItemDetails() { 
    id = 0; 
    menueId = 0; 
    type = 0; 
    styleId= 0; 
    lineBefore= 0; 
    webSite=""; 
    title= ""; 
    icon=""; 
} 

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
    public MenueItemDetails createFromParcel(Parcel in) { 
     return new MenueItemDetails(in); 
    } 

    public MenueItemDetails[] newArray(int size) { 
     return new MenueItemDetails[size]; 
    } 
}; 

public int getLineBefore() { 
    return lineBefore; 
} 

public void setLineBefore(int lineBefore) { 
    this.lineBefore = lineBefore; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public void setMenueId(int menueId) { 
    this.menueId = menueId; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public void setIcon(String icon) { 
    this.icon = icon; 
} 

public void setType(int type) { 
    this.type = type; 
} 

public void setTypeId(int typeId) { 
    this.typeId = typeId; 
} 

public void setStyleId(int styleId) { 
    this.styleId = styleId; 
} 

public int getId() { 
    return id; 
} 

public String getWebSite() { 
    return webSite; 
} 

public void setWebSite(String webSite) { 
    this.webSite = webSite; 
} 

public int getMenueId() { 
    return menueId; 
} 

public String getTitle() { 
    return title; 
} 

public String getIcon() { 
    return icon; 
} 

public int getType() { 
    return type; 
} 

public int getTypeId() { 
    return typeId; 
} 

public int getStyleId() { 
    return styleId; 
} 

}

回答

1

你的第二個活動必須是這樣的:

Intent intent = getIntent(); 
intent.setExtrasClassLoader(MenueItemDetails.class.getClassLoader()); 
myArray = (MenueItemDetails[]) intent.getParcelableArrayExtra("menue"); 
1

如果你的任務是將數據傳遞從活動到活動或片段

比,而不是使用Parcelable可以使用序列化對象,並把它傳遞給意圖很需要更少的時間來實現和代碼比Parcelable

https://stackoverflow.com/a/39631604/4741746

實現你的類序列化

 public class Place implements Serializable{ 
     private int id; 
     private String name; 

     public void setId(int id) { 
      this.id = id; 
     } 
     public int getId() { 
      return id; 
     } 
     public String getName() { 
      return name; 
     } 

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

然後你就可以在意向

 Intent intent = new Intent(this, SecondAct.class); 
    intent.putExtra("PLACE", Place); 
    startActivity(); 

傳遞這個對象INT第二個活動,你可以得到這樣

 Place place= (Place) getIntent().getSerializableExtra("PLACE"); 
1

你的代碼數據傳遞的第一個活動代碼ArrayList中是不正確的在您的活動中發送陣列列表如下:

第一活動代碼

intent.setExtrasClassLoader(MenueItemDetails.class.getClassLoader()); 
       intent.putParcelableArrayListExtra("menue",myArray); 

並在第二次活動中收到如下所示的數組列表。

次活動守則

ArrayList<MenueItemDetails> myarray=getIntent().getParcelableArrayListExtra("menue"); 

與您的代碼的問題是,它是用來發送和接收單一的對象,而不是arraylist.If你還有使用Parceable對象的問題,確保使用Android Parceable Code生成器 Android Studio插件。