Write a sub class of Parcelable to another Parcel

前端 未结 3 2092
盖世英雄少女心
盖世英雄少女心 2020-12-23 09:15

I have a class that implements Parcelable interface:

class A implements Parcelable {

}

I have another class B th

3条回答
  •  心在旅途
    2020-12-23 09:34

    The line:

    obj = (A)in.readParcelable(A.class.getClassLoader());
    

    should change to:

    obj = (A)in.readParcelable(B.class.getClassLoader());
    

    I encountered the same problem and did several Google search, many webpage or tutorial suggests we use A.class.getClassLoader() because we are casting to A, but actually it is WRONG because you will get a null value, we must use B.class.getClassLoader() because the codes are INSIDE class B. I just did not know why but it can get the correct A object in this way.

    Welcome to comment and verify!!

提交回复
热议问题