使用getIntent()时,在偏移量YYY处解组未知类型代码XXXXX.getExtras()。getInt()

时间:2018-01-15 08:15:50

标签: android android-intent

我试图将活动A中的int值传递给活动B.

活动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);

活动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));
        }
    }

帐户(这是列表项):

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

抛出错误:

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)

ActivityB调用intent.getIntExtra("list_size", 0)时,会出现上述错误。如果我删除了ActivityA中的for循环,那就是对象没有传递给ActivityB,那么就不会再出现错误了。

2 个答案:

答案 0 :(得分:0)

你正在做点什么而是得到别的东西。

您正在放置Int

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

现在在Target Activity上执行此操作:

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

并且您的SomeObject类必须实现Parceable接口

答案 1 :(得分:0)

首先使用intent.getExtras()获取Bundle Extras,它返回包含键值对的bundle对象,然后尝试查找不在其中的键list_size对。所以你应该使用intent.getIntExtra()代替。

<强> UPD 我不认为您的包裹实施应该因为套装而有效。使用Set.toArray()方法,然后在Parcel.writeTypedArray中将其写入parcel。