2016-12-26 154 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插件。