简体   繁体   中英

Parceling ArrayList of Integers Android

I have a custom class I want to pass to another Activity and I've read that a fine way to do this is by using Parcelable interface.

public class MatchData implements Parcelable{

    private long mId;
    private int kills, assists, deaths, creeps, ssp1, ssp2, playerRole;
    private String type;
    private String champion;
    private ArrayList<Integer> items;
    private boolean won;

    MatchData(long m, int k, int d, int a, String id, int cr, int s1, int s2, int pr, String t, ArrayList<Integer> it, boolean w) {
        mId = m;
        kills = k;
        assists = a;
        deaths = d;
        champion = id;
        creeps = cr;
        ssp1 = s1;
        ssp2 = s2;
        playerRole = pr;
        type = t;
        items = it;
        won = w;
    }

    MatchData(Parcel in) {
        mId = in.readInt();
        kills = in.readInt();
        deaths = in.readInt();
        assists = in.readInt();
        champion = in.readString();
        creeps = in.readInt();
        ssp1 = in.readInt();
        ssp2 = in.readInt();
        playerRole = in.readInt();
        type = in.readString();
        items = new ArrayList<>();
        in.readList(items, null);  // right here
        won = Boolean.valueOf(in.readString());
    }

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

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeLong(this.getmId());
        out.writeInt(this.getKills());
        out.writeInt(this.getDeaths());
        out.writeInt(this.getAssists());
        out.writeString(this.getChampion());
        out.writeInt(this.getCreeps());
        out.writeInt(this.getSsp1());
        out.writeInt(this.getSsp2());
        out.writeInt(this.getPlayerRole());
        out.writeString(this.getType());
        out.writeList(this.getItems());
        out.writeString(String.valueOf(this.isWon()));
    }

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

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

}

I didnt copy the getters and setters but they are there. The problem I face comes when I try to read the arraylist of integers from the parcel (I commented the line).

Edit: I replaced in that line null with Integer.class.getClassLoader() and this is the new stacktrace

06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: FATAL EXCEPTION: main
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: Process: com.nicu.bogdan.lolstats, PID: 2429
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicu.bogdan.lolstats/com.nicu.bogdan.playerInfo.MatchActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@24a39bfa: Unmarshalling unknown type code 4915278 at offset 168
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:  Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@24a39bfa: Unmarshalling unknown type code 4915278 at offset 168
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Parcel.readValue(Parcel.java:2228)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Parcel.readListInternal(Parcel.java:2526)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Parcel.readList(Parcel.java:1661)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.nicu.bogdan.jsonParser.MatchData.<init>(MatchData.java:140)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.nicu.bogdan.jsonParser.MatchData$1.createFromParcel(MatchData.java:167)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.nicu.bogdan.jsonParser.MatchData$1.createFromParcel(MatchData.java:165)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Parcel.readParcelable(Parcel.java:2252)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Parcel.readValue(Parcel.java:2152)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.BaseBundle.unparcel(BaseBundle.java:221)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Bundle.getParcelable(Bundle.java:755)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.content.Intent.getParcelableExtra(Intent.java:5088)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.nicu.bogdan.playerInfo.MatchActivity.onCreate(MatchActivity.java:43)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
06-23 13:26:35.479 2429-2429/com.nicu.bogdan.lolstats E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Integer isn't parcelable by default. Try this:

in.readList(ids, Integer.class.getClassLoader());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM