【发布时间】:2019-06-25 19:33:02
【问题描述】:
好的,我已经阅读了几乎所有关于堆栈溢出的可打包问题,并尝试了多种解决方案来解决这个问题。所以总的来说我有一个android项目,我想通过Intent将2个ArrayLists的数组从一个类发送到另一个类。
注意:这不是嵌套 parcelable 线程 here 的副本,它不处理数组。它不是 this 的副本,因为我已将所有嵌套类都设为 parcelable。它不是 this 或 this 的副本,因为我没有使用数组列表,而且我认识到错误是初始化失败。它不是this 的副本,因为在这种情况下我不需要无参数构造函数(我已经测试过添加一个并且它不会改变错误)。它也不是this 的副本,因为它不是嵌套在包裹中的包裹。
我必须做一些显而易见的事情,因为只有在我的数组尚未初始化时才会出现空指针错误。但是,在我知道我希望它有多长之前,我无法初始化我的数组长度......
(作为旁注,也许有人可以让我更深入地了解readTypedList 类以及XXX.Creator 究竟做了什么,因为这无疑可能是问题的一部分。对于任何未来有同样问题的人here是指向Parcel javadoc 的链接,它提供了丰富的信息,但没有解决我的问题。)
所以现在我有了一个 Display 对象。 Display 对象仅用于存储String[] 并允许对其进行打包。与 断开的行已在下面进行了注释,对于任何有包裹经验的人来说都应该是显而易见的,因为它显然没有被初始化:
class Display implements Parcelable {
String[] labels;
public Display(String[] labels) {
this.labels = labels;
}
protected Display(Parcel in) {
in.readStringArray(this.labels); // **** THIS LINE BREAKS
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(this.labels);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Display> CREATOR = new Creator<Display>() {
@Override
public Display createFromParcel(Parcel in) {
return new Display(in);
}
@Override
public Display[] newArray(int size) {
return new Display[size];
}
};
}
所以现在我问自己我能做些什么来解决这个问题,并调查了许多其他有希望名字的线程,但他们没有解决我的问题。
Here 是一个我本来会提供帮助的答案,同样here 他们建议使用createTypedList() 而不是readTypedList()。
但是,我尝试了第一个答案和this,但它们没有工作,因为我事先不知道我希望我的String[] 存在多长时间,这最终导致了一个 错误的数组长度错误。第二个答案没有帮助,因为显然我不想创建一个新的类型列表,而是使用我已经拥有的类型列表,所以通过创建一个新列表,我最终得到一个空白列表并丢失了我的数据。
所有这些问题的根源在于我的包裹嵌套在另一个包裹中并通过以下方式调用:
in.readTypedList(allChartLabels, Display.CREATOR);
因为它是嵌套的,所以调用 CREATOR 的方式极大地限制了我的选择,导致我无法解决问题。
在各种测试中,我遇到了许多错误,但没有解决方案...(我的代码当前抛出的错误是 Attempt to get length of null array 错误)。
我知道有人对此问题有解决方案,更多详细信息请参见我的其余代码:
public class SummaryEntry implements Parcelable {
private ArrayList<Calculation> allChartValues; // NOTE: this is another nested parcel class which is basically a duplicate of Display but with float[]
private ArrayList<Display> allChartLabels;
public SummaryEntry() {
// initialize
allChartValues = new ArrayList<>();
allChartLabels = new ArrayList<>();
}
protected SummaryEntry(Parcel in) {
this();
// order in which we do this matters:
in.readTypedList(allChartLabels, Display.CREATOR);
in.readTypedList(allChartValues, Calculation.CREATOR);
}
@Override
public void writeToParcel(Parcel out, int i) {
// order in which we do this matters, must be same as reading typed lists
out.writeTypedList(allChartLabels);
out.writeTypedList(allChartValues);
}
/// ... getters and setters excluded because of irrelevance ///
/// PARCELABLE METHODS
@Override
public int describeContents() {
return 0;
}
public static final Creator<SummaryEntry> CREATOR = new Creator<SummaryEntry>() {
@Override
public SummaryEntry createFromParcel(Parcel in) {
return new SummaryEntry(in);
}
@Override
public SummaryEntry[] newArray(int size) {
return new SummaryEntry[size];
}
};
}
感谢您抽出宝贵时间阅读这篇长文。理想情况下,我正在寻找String[] 初始化问题的解决方案,或者是否有人可以为包含数组的嵌套包裹发布他们的工作代码,或者最后也许有人可以指出一种更简单的方法来实现传递这些项目而无需嵌套两个包裹.
【问题讨论】:
标签: java android android-intent parcelable