我有以下课程:
public class DetailedProduct implements Serializable {
//attributes + get and set
private Colour colour;
//get+set
public class Colour implements Serializable{
private ArrayList<Image> images;
//get+set
public Image[] getImages() {
return images.toArray(new Image[images.size()]);
}
}
public class Image implements Serializable{
private static final long serialVersionUID = 3460333138445770749L;
private String image1;
private String image2;
private String image3;
//get/set methods
}
}
我后来创建了一个 Intent 如下
DetailedProduct.Colour mCurrentColour;
Intent myIntent = new Intent(DetailsActivity.this, ImageGallery.class);
myIntent.putExtra("Images", mCurrentColour.getImages());
startActivity(myIntent);
现在在 ImageGallery 类中,我尝试了以下代码:
Serializable extras = getIntent().getSerializableExtra("Images");
if (extras != null) {
images = (Image[]) extras;
}
但是我遇到了以下异常:java.lang.RuntimeException:无法启动活动 ComponentInfo{com./com.productdetails.ProductImageGallery}:java.lang.ClassCastException:java.lang.Object[] 无法转换为 com。 productdetails.DetailedProduct$Image[]
如何正确地将可序列化转换为 Image 数组