Question

I have an ArrayList of a custom class to be passed from an activity to another. I make the class implements Parcelable. However, every time when I use the passed ArrayList I get NullPointException. So I decide to check the class of the return value of getParcelableArrayList() and I got java.lang.Integer. I wonder how to explain this.

Code of the first Activity:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(this, WeiboBrowseActivity.class);
    intent.putParcelableArrayListExtra(WeiboBrowseActivity.KEY_WEIBO_INFO_LIST, weiboInfoList);
    intent.putExtra(WeiboBrowseActivity.KEY_SELECTED_WEIBO_INDEX, arg2);
    startActivity(intent);
    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}

Code of the second Activity:

private void initWeiboInfos() {
    Log.d("cosmo","the type is: " + getIntent().getExtras().get(KEY_WEIBO_INFO_LIST).getClass().getName());
    weiboInfos = getIntent().getExtras().getParcelableArrayList(KEY_WEIBO_INFO_LIST);
    if (weiboInfos == null) {
        Log.d("cosmo", "weiboInfos null");     //I got this log
    } else {
        Log.d("cosmo", "weiboInfos not null");
    }
}

Code of the Parcelable Class:

public class WeiboInfo implements Parcelable {
    private String absolutePath;
    private long createTimestamp;

    public WeiboInfo() {
        super();
        this.setAbsolutePath("");
        this.setCreateTimestamp(new Date().getTime());
    }

    public WeiboInfo(String absolutePath, long createTimestamp) {
        super();
        this.setAbsolutePath(absolutePath);
        this.setCreateTimestamp(createTimestamp);
    }

    public WeiboInfo(Parcel in) {
        super();
        this.setAbsolutePath(in.readString());
        this.setCreateTimestamp(in.readLong());
    }

    public String getAbsolutePath() {
        return absolutePath;
    }

    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public long getCreateTimestamp() {
        return createTimestamp;
    }

    public void setCreateTimestamp(long createTimestamp) {
        this.createTimestamp = createTimestamp;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeString(absolutePath);
        dest.writeLong(createTimestamp);
    }

    public void readFromParcel(Parcel in) {
        absolutePath = in.readString();
        createTimestamp = in.readLong();
    }

    public static final Parcelable.Creator<WeiboInfo> CREATOR = new Parcelable.Creator<WeiboInfo>() {
        public WeiboInfo createFromParcel(Parcel in) {
            return new WeiboInfo(in);
        }

        public WeiboInfo[] newArray(int size) {
            return new WeiboInfo[size];
        }
    };
}

Here are the two keys:

    public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST";
    public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_WEIBO_INFO_LIST";
Was it helpful?

Solution

public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST";
public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_WEIBO_INFO_LIST";

it works like a dictionary. You can not have duplicate keys. That's why you are getting the int, because here

intent.putParcelableArrayListExtra(WeiboBrowseActivity.KEY_WEIBO_INFO_LIST, weiboInfoList);
 intent.putExtra(WeiboBrowseActivity.KEY_SELECTED_WEIBO_INDEX, arg2);

the latter is overriding the former. Change it in

public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST";
public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_SELECTED_WEIBO_INDEX";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top