包含自身ArrayList的Parcelable

时间:2015-08-04 16:00:04

标签: java android arraylist parcelable parcel

我正在尝试将一个类别列表从一个Activity传递给另一个。我已经在课堂上实现了Parcelable,但每次都会崩溃应用程序。注意:活动在没有传递额外的情况下有效。 注2:我看不到任何日志,只是如此缓慢而突然“D / AndroidRuntime:关闭虚拟机”

这是包裹:

public  class   Category implements Parcelable{

private String categoryId;
private String name;
private boolean active;
private String slug;
private String externalId;
private String parentId;
private Integer level;
private String categoryTreeNodeId;
private ArrayList<Category> children;


public Category() {}

// Getters and Setters...

@Override
public int describeContents() {
    // ignore for now
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(categoryId);
    dest.writeString(name);
    dest.writeByte((byte) (active ? 1 : 0));
    dest.writeString(slug);
    dest.writeString(externalId);
    dest.writeString(parentId);
    dest.writeInt(level);
    dest.writeString(categoryTreeNodeId);
    dest.writeTypedList(children);
}

/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
    public Category createFromParcel(Parcel pc) {
        return new Category(pc);
    }
    public Category[] newArray(int size) {
        return new Category[size];
    }
};

/**Ctor from Parcel, reads back fields IN THE ORDER they were written */
public Category(Parcel in){
    categoryId = in.readString();
    name = in.readString();
    active = in.readByte() != 0;
    slug = in.readString();
    externalId = in.readString();
    parentId = in.readString();
    level =  in.readInt();
    categoryTreeNodeId = in.readString();
    in.readTypedList(children, Category.CREATOR);
}

}

这就是我称之为活动的方式:

ArrayList<Category> list = currentCategory.getChildren();
    intent.putParcelableArrayListExtra(SubCategoryActivity.EXTRA_CATEGORY_LIST, list);
    intent.putExtra(SubCategoryActivity.EXTRA_CURRENT_CAT_NAME, currentCategory.getName());
    context.startActivity(intent);

EXTRA_CURRENT_CAT_NAME只是一个字符串,它运作正常。

我相信问题出在ArrayList中。有什么建议或经验做类似的事情吗?

感谢。

我得到的唯一日志是:

  

08-04 17:56:21.529 30543-30543 / com.example.test D / AndroidRuntime:关闭VM   08-04 17:56:41.554 30543-30552 / com.example.test I / art:Thread [5,tid = 30552,WaitingInMainSignalCatcherLoop,Thread * = 0xb81e22d0,peer = 0x12c00080,“Signal Catcher”]:对信号3作出反应   08-04 17:56:41.554 30543-30552 / com.example.test I / art:[08-04 17:56:41.754 787:809 I / Process]       发送信号。 PID:787 SIG:3

1 个答案:

答案 0 :(得分:1)

您的问题是由将类型列表读入空容器引起的:

protected override DbEntityValidationResult ValidateEntity(
  DbEntityEntry entityEntry,
  IDictionary<object, object> items)
{
  var result = base.ValidateEntity(entityEntry, items);
  var falseErrors = result.ValidationErrors
    .Where(error =>
    {
      var member = entityEntry.Member(error.PropertyName);
      var property = member as DbPropertyEntry;
      if (property != null)
        return !property.IsModified;
      else
        return false;//not false err;
    });

  foreach (var error in falseErrors.ToArray())
    result.ValidationErrors.Remove(error);
  return result;
}

它有点令人困惑,但in.readTypedList(children, Category.CREATOR); 的用例要求传入的readTypedList参数为非null。我建议改用它来使用:

List

如果列表在写入包裹时为空,这也将保持空值。