getParcelableArrayList返回null

时间:2014-03-28 14:02:02

标签: java android parcelable

我有一个自定义类的ArrayList要从一个活动传递到另一个活动。我让类实现Parcelable。但是,每当我使用传递的ArrayList时,我都会获得NullPointException。  所以我决定检查getParcelableArrayList()的返回值的类,我得到了java.lang.Integer。我想知道如何解释这个。

第一个活动的代码:

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

第二项活动的代码:

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

Parcelable类的代码:

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

以下是两个键:

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

1 个答案:

答案 0 :(得分:4)

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

它像字典一样工作。您不能拥有重复的密钥。这就是你获得int的原因,因为这里

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

后者超越了前者。

中改变它
public static final String KEY_WEIBO_INFO_LIST = "KEY_WEIBO_INFO_LIST";
public static final String KEY_SELECTED_WEIBO_INDEX = "KEY_SELECTED_WEIBO_INDEX";