私は別のアクティビティに渡したいカスタムクラスを持っています。これを行ううまい方法はParcelableインターフェイスを使用することです。整数のArrayListを分割するAndroid
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に置き換え)、これは新しいスタックトレース
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 [email protected]: 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 [email protected]: 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)
の代わりに、ヌル、readList( 'Integer.class.getClassLoader()')に整数のクラスローダを渡し – Blackbelt
:これを試してみてください。 – m0skit0
'mId = in.readInt();'はmId = in.readLong();でなければなりません。 – Blackbelt