Java 嵌套的 parcelable,无法初始化数组

Java nested parcelable, can't initialize an array

好的,我已经通读了几乎所有关于堆栈溢出的可打包问题,并尝试了多种解决方案来解决这个问题。所以总的来说,我有一个 android 项目,我想通过 Intent 从一个 class 向另一个发送 2 ArrayList 个数组。

注意:这不是嵌套可打包线程的副本here which does not deal with arrays. It is not a duplicate of this because I have made all nested classes parcelable. It is not a duplicate of this or this because I am not using an array list and also I recognize the error is a failure of initializing. It is not a duplicate of because I do not need a parameter-less constructor in this situation (I have tested adding one and it doesn't change the error). It is also not a duplicate of this,因为它不是嵌套在包裹中的包裹。

我一定是在做一些显而易见的事情,因为空指针错误只会在我的数组尚未初始化时发生。但是,我无法初始化我的数组长度,直到我知道我想要它有多长...

(作为旁注,也许有人可以让我更深入地了解 readTypedList class 以及 XXX.Creator 究竟做了什么,因为这无疑可能是问题的一部分。也为任何有相同问题 here 的未来人都是 Parcel javadoc 的 link,它提供了信息但没有解决我的问题。)

现在我有了一个 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 was an answer I would have though would have helped, similarly 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];
        }
    };
}

感谢您抽出宝贵时间阅读这么长的内容 post。理想情况下,我正在寻找 String[] 初始化问题的解决方案,或者如果有人可以 post 他们的工作代码用于包含数组的嵌套包裹,或者最后也许有人可以指出一种更简单的方法来实现传递这些物品没有嵌套两个包裹。

你可以像下面的代码那样做:

像这样使用 writeArray 和 readArray:

尝试使用以下更新的代码:

package com.myapplication;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable {

    String name;
    int age;
    String [] array;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeInt(this.age);
        dest.writeStringArray(this.array);
    }

    public User() {
    }

    protected User(Parcel in) {
        this.name = in.readString();
        this.age = in.readInt();
        this.array = in.createStringArray();
    }

    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            return new User(source);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
}

我仍然无法解决原始问题的后半部分(即如何包裹数组),但通过稍微重新设计我的程序,我能够成功实现嵌套包裹实施。

新设计将实现分解为 4 个嵌套的 classes,实现简单明了。我还删除了我的数组,因为我无法在包裹中正确初始化它们。

编辑:我也偶然发现了 this website,它会自动为您创建 parcelables。非常有用,我希望我以前知道它! 另请注意,显然没有必要为包裹 StringFloat 创建包裹 classes。您可以改为调用 Float.class.getClassLoader()String.class.getClassLoader() 为那些 classes.

使用 parcel CREATORS

这是我的代码:

Parcelable class Q 包含 ArrayList of:

  • Parcelable class A 包含 2 个字符串和浮点数的数组列表

通过将包裹分解成更小的嵌套包裹,我能够逐步测试包裹,直到找到最终解决方案,该解决方案可以通过我的 Intent 通过此代码传递:

在发送端:

    // create the parcel
    Q parcel = new Q();

    ArrayList<String> strings = new ArrayList<>();
    ArrayList<Float> stats = new ArrayList<>();

    // ... populate arraylists ... //

    Intent I = new Intent(CURRENTACTIVITY.this, FUTUREACTIVITY.class);
    // PUT THE THING
    I.putExtra("Data", parcel);

    startActivity(I);

在接收端:

    Q parcel = getIntent().getParcelableExtra("Data"); // get data

工作包裹的完整代码: 这段代码很长且重复,但设置包裹的基本原则很清楚,如果您想创建自己的包裹,也很容易重复使用。

// top level parcelable class Q
public class Q  implements Parcelable {

    private ArrayList<A> element;

    public Q()
    {
        this.element = new ArrayList<A>();
    }

    public void addToA(A a)
    {
        element.add(a);
    }

    public ArrayList<A> getA() {
        return element;
    }

    public void setA(ArrayList<A> a) {
        this.element = a;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeTypedList(this.element);

    }

    private Q (Parcel in){
        element = new ArrayList<A>();
        in.readTypedList(this.element, A.CREATOR);
    }

    public static final Parcelable.Creator<Q> CREATOR = new Parcelable.Creator<Q>() {
        public Q createFromParcel(Parcel in) {
            return new Q(in);
        }

        public Q[] newArray(int size) {
            return new Q[size];
        }
    };

}

// nested parcel object A
public class A implements Parcelable {

    private ArrayList<Float> f;
    private ArrayList<String> s;

    public A() {
        f = new ArrayList<>();
        s = new ArrayList<>();
    }

    public A(ArrayList<Float> f, ArrayList<String> s) {
        this.f = f;
        this.s = s;
    }

    public ArrayList<String> getS() {
        return s;
    }

    public void setS(ArrayList<String> s) {
        this.s = s;
    }

    public ArrayList<Float> getF() {
        return f;
    }

    public void setF(ArrayList<Float>  f) {
        this.f = f;
    }


    protected A(Parcel in) {
        if (in.readByte() == 0x01) {
            f = new ArrayList<>();
            in.readList(f, Float.class.getClassLoader());
        } else {
            f = null;
        }
        if (in.readByte() == 0x01) {
            s = new ArrayList<>();
            in.readList(s, String.class.getClassLoader());
        } else {
            s = null;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (f == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(f);
        }
        if (s == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(s);
        }
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
        @Override
        public A createFromParcel(Parcel in) {
            return new A(in);
        }

        @Override
        public A[] newArray(int size) {
            return new A[size];
        }
    };
}