0

男孩,这是一个奇怪的。

我正在向服务发送意图。

public void updatePortfolio(VehiclePortfolio vehiclePortfolio) {
    GenericParcel gp = new GenericParcel(vehiclePortfolio);
    Intent apiIntent = new Intent(context, ApiRequestService.class);
    apiIntent.putExtra(Constants.ARG_REQUEST, Constants.REQUEST_UPDATE_PORTFOLIO);
    apiIntent.putExtra(Constants.ARG_VEHICLE_PORTFOLIO, gp);
    // Try getting object back here
    //gp = apiIntent.getParcelableExtra(Constants.ARG_VEHICLE_PORTFOLIO);
    VehiclePortfolio vp = (VehiclePortfolio)gp.getObject();
    String s = apiIntent.getStringExtra(Constants.ARG_REQUEST);
    vehiclePortfolio = (VehiclePortfolio) gp.getObject();

    // .putExtra(Constants.ARG_VEHICLE_PORTFOLIO, b);
    context.startService(apiIntent);
}

当我在此函数末尾检查意图中的附加内容时,它们看起来很好,如下所示。

 mMap   HashMap  (id=830037731672)  
[0] HashMap$HashMapEntry  (id=830037731800) 
    key "ARG_VEHICLE_PORTFOLIO" (id=830038543392)   
    value   GenericParcel  (id=830038204888)    
        o   VehiclePortfolio  (id=830038244704) 
            configuration   VehicleConfiguration  (id=830038258216) 
            id  "1375379159508" (id=830038245672)   
            optionals   VehiclePortfolio$_Fields[5]  (id=830038244744)  
            priceReport VehiclePriceReport  (id=830038312960)   
            quotes  ArrayList  (id=830038417704)    
            timestamp   "2013-08-01T17:46:31.000Z" (id=830038536424)    
[1] HashMap$HashMapEntry  (id=830037731768) 
    key "ARG_REQUEST" (id=830037687416) 
    value   Integer  (id=830019251512)  
        value   6   

然后,当服务接收到意图时,这就是附加组件的样子。请注意,vehiclePortfolio 已在 ARG_REQUEST extra 之上被覆盖,并且键和值已被交换。(什么?!?)

  mMap  HashMap  (id=830038833832)  
[0] HashMap$HashMapEntry  (id=830039001656) 
    key VehiclePortfolio  (id=830038934408) 
        configuration   VehicleConfiguration  (id=830038936272) 
        id  "1375379159508" (id=830038936128)   
        optionals   null    
        priceReport VehiclePriceReport  (id=830038941176)   
        quotes  ArrayList  (id=830038963264)    
        timestamp   "2013-08-01T17:46:31.000Z" (id=830039001424)    
    value   "ARG_REQUEST" (id=830039001576) 
[1] HashMap$HashMapEntry  (id=830038834568) 
    key "ARG_VEHICLE_PORTFOLIO" (id=830038833888)   
    value   GenericParcel  (id=830038834512)    
        o   Parcel  (id=830037350528)   
            mNativePtr  1891558544  
            mOwnsNativeParcelObject true    
            mStack  null    

下面是我怀疑是罪魁祸首的 GenericParcel 的代码。如果我进行测试并传递一个字符串来代替 GenericParcel 中的复杂对象,则行为符合预期。

public class GenericParcel implements Parcelable {
    private Object o;

    public GenericParcel(Object in) {
        o = in;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeValue(o);
    }

    public Object getObject() {
        return o;
    }

    public static final Creator<GenericParcel> CREATOR = new Creator<GenericParcel>() {

        public GenericParcel createFromParcel(Parcel source) {
            return new GenericParcel(source);
        }

        public GenericParcel[] newArray(int size) {

            return new GenericParcel[size];
        }
    };

}
4

1 回答 1

1

我认为您对Parcelable对象的实现不正确。您应该有一个构造函数,该构造函数将 a Parcel(不是 an Object)作为其唯一参数,并且您应该按照将值Parcel写入 in 的相同顺序从 theParcel中读取值writeToParcel()

public class GenericParcel implements Parcelable {
    private Object o;

    public GenericParcel(Parcel in) {
        o = in.readValue(null);
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeValue(o);
    }

    // the rest removed for brevity
}
于 2013-08-11T17:05:28.623 回答