整数Android的Parceling ArrayList

时间:2016-06-23 10:14:04

标签: java android arraylist parcelable parcel

我有一个我希望传递给另一个Activity的自定义类,我已经读过,使用Parcelable接口这样做的好方法。

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];
        }
    };

}

我没有复制吸气剂和制定者,但他们在那里。当我尝试从包裹中读取整数的arraylist时,我面临的问题就出现了。(我对该行进行了评论)。

编辑:我在那行中用Integer.class.getClassLoader()替换了null,这是新的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) 

1 个答案:

答案 0 :(得分:0)

默认情况下,整数是不可分割的。试试这个:

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