本文介绍了读取和放大器; Parcelable对象的写作阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的类读取和写入对象的数组从/到一个包裹:

I have following class which reads and writes an array of objects from/to a parcel:

class ClassABC extends Parcelable {
    MyClass[] mObjList;

    private void readFromParcel(Parcel in) {
        mObjList = (MyClass[]) in.readParcelableArray(
                com.myApp.MyClass.class.getClassLoader()));
    }

    public void writeToParcel(Parcel out, int arg1) {
        out.writeParcelableArray(mObjList, 0);
    }

    private ClassABC(Parcel in) {
        readFromParcel(in);
    }

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<ClassABC> CREATOR =
            new Parcelable.Creator<ClassABC>() {

        public ClassABC createFromParcel(Parcel in) {
            return new ClassABC(in);
        }

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

在上面的code,我收到了 ClassCastException异常阅读时 readParcelableArray

In above code I get a ClassCastException when reading readParcelableArray:

ERROR / AndroidRuntime(5880):java.lang.ClassCastException:产生的原因[Landroid.os.Parcelable;

什么是错在上述code?在写对象的数组,我应该首先将阵列的的ArrayList

What is wrong in above code? While writing the object array, should I first convert the array to an ArrayList?

更新:

是否确定对象数组转换为的ArrayList ,并将其添加到包裹?例如,书写时:

Is it OK to convert an Object array to an ArrayList and add it to parcel? For instance, when writing:

    ArrayList<MyClass> tmpArrya = new ArrayList<MyClass>(mObjList.length);
    for (int loopIndex=0;loopIndex != mObjList.length;loopIndex++) {
        tmpArrya.add(mObjList[loopIndex]);
    }
    out.writeArray(tmpArrya.toArray());

当阅读:

    final ArrayList<MyClass> tmpList =
            in.readArrayList(com.myApp.MyClass.class.getClassLoader());
    mObjList= new MyClass[tmpList.size()];
    for (int loopIndex=0;loopIndex != tmpList.size();loopIndex++) {
        mObjList[loopIndex] = tmpList.get(loopIndex);
    }

但现在我得到一个 NullPointerException异常。是上面的做法是正确的?为什么它是抛出一个NPE?

But now I get a NullPointerException. Is above approach is correct? Why it is throwing an NPE?

推荐答案

您需要使用 Parcel.writeTypedArray()方法写入阵列,并与读回 Parcel.createTypedArray()的方法,像这样:

You need to write the array using the Parcel.writeTypedArray() method and read it back with the Parcel.createTypedArray() method, like so:

MyClass[] mObjList;

public void writeToParcel(Parcel out) {
    out.writeTypedArray(mObjList, 0);
}

private void readFromParcel(Parcel in) {
    mObjList = in.createTypedArray(MyClass.CREATOR);
}

为什么你不应该使用 readParcelableArray() / writeParcelableArray()方法的原因是, readParcelableArray()真正创建一个 Parcelable [] 结果。这意味着你可以不投的方法的结果为 MyClass的[] 。相反,你必须创建相同的长度作为结果的 MyClass的数组和复制的每个元素从结果数组的 MyClass的阵列。

The reason why you shouldn't use the readParcelableArray()/writeParcelableArray() methods is that readParcelableArray() really creates a Parcelable[] as a result. This means you cannot cast the result of the method to MyClass[]. Instead you have to create a MyClass array of the same length as the result and copy every element from the result array to the MyClass array.

Parcelable[] parcelableArray =
        parcel.readParcelableArray(MyClass.class.getClassLoader());
MyClass[] resultArray = null;
if (parcelableArray != null) {
    resultArray = Arrays.copyOf(parcelableArray, parcelableArray.length, MyClass[].class);
}

这篇关于读取和放大器; Parcelable对象的写作阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:24