I'm trying to pass an int value from activity A to activity B.
Activity A:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("list_size", list.size());
for (int i = 0; i < list.size(); i++) {
intent.putExtra("account" + i, accountList.get(i));
}
startActivityForResult(intent, 2);
Activity B:
private void init() {
Intent intent = getIntent();
int listSize = intent.getIntExtra("list_size", 0); // Error thrown here
for (int i = 0; i < listSize; i++) {
newList.add((Account) intent.getParcelableExtra("account" + i));
}
}
Account (this is the list item):
public class Account implements Parcelable {
private Long accountId;
private String accountName;
private Set followedAccountsIds = new HashSet<>();
private Set followersAccountsIds = new HashSet<>();
private Set likedTagsIds = new HashSet<>();
private Set postsByAccount = new HashSet<>();
public Account() {
}
public Account(Long accountId, String accountName, Set followedAccountsIds,
Set followersAccountsIds, Set likedTagsIds, Set postsByAccount) {
this.accountId = accountId;
this.accountName = accountName;
this.followedAccountsIds = followedAccountsIds;
this.followersAccountsIds = followersAccountsIds;
this.likedTagsIds = likedTagsIds;
this.postsByAccount = postsByAccount;
}
// Omitted getters and setters for brevity
protected Account(Parcel in) {
if (in.readByte() == 0) {
accountId = null;
} else {
accountId = in.readLong();
}
accountName = in.readString();
}
public static final Creator CREATOR = new Creator() {
@Override
public Account createFromParcel(Parcel in) {
return new Account(in);
}
@Override
public Account[] newArray(int size) {
return new Account[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(accountId);
parcel.writeString(accountName);
}
}
Error that is thrown:
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@8b4252e: Unmarshalling unknown type code 6357091 at offset 140
at android.os.Parcel.readValue(Parcel.java:2453)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2727)
at android.os.BaseBundle.unparcel(BaseBundle.java:269)
at android.os.BaseBundle.getInt(BaseBundle.java:867)
at android.content.Intent.getIntExtra(Intent.java:6637)
at com.david.songshareandroid.activities.ActivityB.init(ActivityB.java:33)
at com.david.songshareandroid.activities.ActivityB.onCreate(ActivityB.java:28)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
In ActivityB when intent.getIntExtra("list_size", 0) is invoked, the error above occurs. If I remove the for loop in ActivityA, that is the objects are not getting passed to ActivityB, then the error does not occur anymore.