Android WriteParceableArray抛出ClassCastException

Android WriteParceableArray抛出ClassCastException,android,parcelable,Android,Parcelable,我在将对象集合写入包裹时遇到了一个问题。我在MyRegistry中存储了一个MyObject集合(即实现Parcelable的任何对象),该集合也必须是可定步长的: public class MyObject implements Parcelable {...} public class MyRegistry implements Parcelable { private List<Parcelable> mRegistry = new ArrayList<Parce

我在将对象集合写入包裹时遇到了一个问题。我在MyRegistry中存储了一个MyObject集合(即实现Parcelable的任何对象),该集合也必须是可定步长的:

public class MyObject implements Parcelable {...}

public class MyRegistry implements Parcelable {

  private List<Parcelable> mRegistry = new ArrayList<Parcelable>();

  ...

  public void writeToParcel(Parcel dest, int flags)
  {
    dest.writeParcelableArray((Parcelable[])mRegistry.toArray(), flags);
  }
}
public类MyObject实现了Parcelable{…}
公共类MyRegistry实现了Parcelable{
private List mRegistry=new ArrayList();
...
公共无效写入包裹(包裹目的地,内部标志)
{
dest.writeParcelableArray((可包裹[])mRegistry.toArray(),标志);
}
}
但是,调用writeParcelableArray()时会引发ClassCastException。那么,写一个或多个可包裹的MyObject有什么错呢?没有满足文档中关于此的任何限制


提前谢谢你

我认为这是因为返回了一个Object[]数组,然后您尝试对其进行强制转换。你应该改用。

你完全正确!谢谢!现在还不清楚的另一件事是readParcelableArray的ClassLoader参数。Registry类不知道存储在其中的对象的实际类型,它们只是可打包的…Uff,答案来自。我使用了应用程序上下文中的类加载器:getContext().getClassLoader()。无需将mRegistry强制转换为
(Parcelable[])