2

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<String> followedAccountsIds = new HashSet<>();
    private Set<String> followersAccountsIds = new HashSet<>();
    private Set<Integer> likedTagsIds = new HashSet<>();
    private Set<Post> postsByAccount = new HashSet<>();


    public Account() {
    }

    public Account(Long accountId, String accountName, Set<String> followedAccountsIds,
                   Set<String> followersAccountsIds, Set<Integer> likedTagsIds, Set<Post> 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<Account> CREATOR = new Creator<Account>() {
        @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.

cod3min3
  • 585
  • 1
  • 7
  • 23

2 Answers2

0

You are putting something but getting something else.

You are putting an Int

intent.putExtra("list_size", list.size());

Now on Target Activity do this:

Intent intent = getIntent();
int number = intent.getIntExtra("list_size",0);

and your SomeObject class must implement Parceable interface

Zaid Mirza
  • 3,540
  • 2
  • 24
  • 40
0

First you taking Bundle Extras using intent.getExtras() which returns you bundle object which contains key-value pairs, and then you trying to find pair with key list_size which is not in there. So you should use intent.getIntExtra() instead.

UPD I don't think that your parcel implementation should work because of Sets. Use Set.toArray() method, then in Parcel.writeTypedArray write it to the parcel.

Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34