0

我正在尝试使我的对象 Parcelable。我有以下代码用于从包裹创建对象以及写入包裹的函数。当尝试从包裹创建对象时,我在创建 m_endTime 时遇到错误,它说它不能将字符串转换为 Long。不太清楚发生了什么。

private RECTripFishery(Parcel in) {
    m_tripId = (Integer) in.readValue(null);
    m_fishery = (String) in.readValue(null);
    m_targetSpecies = (String) in.readValue(null);
    m_startTime = new Date((Long) in.readValue(null));
    if (in.readValue(null) == null){
        m_endTime = null;
    } else {
        m_endTime = new Date((Long) in.readValue(null));
    }
    m_startLatitude = (Double) in.readValue(null);
    m_startLongitude = (Double) in.readValue(null);
    m_endLatitude = (Double) in.readValue(null);
    m_endLongitude = (Double) in.readValue(null);
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(Integer.valueOf(m_tripId));
    dest.writeValue(m_fishery);
    dest.writeValue(m_targetSpecies);
    dest.writeValue(Long.valueOf(m_startTime.getTime()));
    if (m_endTime == null) {
        dest.writeValue(null);
    } else {
        dest.writeValue(Long.valueOf(m_endTime.getTime()));
    }

    dest.writeValue(m_startLatitude);
    dest.writeValue(m_startLongitude);
    dest.writeValue(m_endLatitude);
    dest.writeValue(m_endLongitude);

    return;
}private RECTripFishery(Parcel in) {
    m_tripId = (Integer) in.readValue(null);
    m_fishery = (String) in.readValue(null);
    m_targetSpecies = (String) in.readValue(null);
    m_startTime = new Date((Long) in.readValue(null));
    if (in.readValue(null) == null){
        m_endTime = null;
    } else {
        m_endTime = new Date((Long) in.readValue(null));
    }
    m_startLatitude = (Double) in.readValue(null);
    m_startLongitude = (Double) in.readValue(null);
    m_endLatitude = (Double) in.readValue(null);
    m_endLongitude = (Double) in.readValue(null);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(Integer.valueOf(m_tripId));
    dest.writeValue(m_fishery);
    dest.writeValue(m_targetSpecies);
    dest.writeValue(Long.valueOf(m_startTime.getTime()));
    if (m_endTime == null) {
        dest.writeValue(null);
    } else {
        dest.writeValue(Long.valueOf(m_endTime.getTime()));
    }

    dest.writeValue(m_startLatitude);
    dest.writeValue(m_startLongitude);
    dest.writeValue(m_endLatitude);
    dest.writeValue(m_endLongitude);

    return;
}
4

1 回答 1

0

更改此部分:

if (in.readValue(null) == null) {
        m_endTime = null;
} else {
        m_endTime = new Date((Long) in.readValue(null));
}

至:

Object val = in.readValue(null);

if (val == null) {
    m_endTime = null;
} else {
    m_endTime = new Date((Long) val);
}

编辑:readValue您的问题是两次调用。

于 2013-11-12T18:30:10.217 回答