我正在编写一个运行非本地服务的Android应用程序,以便从互联网上获取数据.我有一个绑定到所述服务的活动,并检索Order对象列表以显示它们.
将列表从服务传递到活动时似乎出现了问题.虽然这对我来说似乎很简单.
The Problem
All the items in the List, but the item at index 1, are null values.
调试显示服务函数具有正确的列表并返回正确的列表:
这是返回列表的服务中的函数:
public List<Order> getorders() throws remoteexception { synchronized(service.orders) { // service.orders returns a hashmap<Integer, Order> // where the key is the ID of the order // The function returns a simple List, so we parse to an ArrayList List<Order> result = new ArrayList<Order>(service.orders.values()); return result; }}
这是Activity中调用服务并检索列表的函数(其中API是绑定到服务的结果):
handler.post(new Runnable() { public voID run() { try { List<Order> result = API.getorders(); displayOrders(result); } catch (Throwable t) { Log.e(TAG, "Error while updating the UI with orders", t); } }});
这让我感到非常难过,因为我还有一个活动来查看客户和一个返回客户列表的服务功能,并且运行完美.两者之间的主要区别在于Customer没有任何自定义对象属性,而Order有一些.
编辑:为Order类添加了Parcelable实现(通过删除大多数原始属性简化):
/** Parcelabe interface implementation*/public static final Creator<Order> CREATOR = new Creator<Order>() { public Order createFromParcel(Parcel source) { return new Order(source); } public Order[] newArray(int size) { return new Order[size]; }};public Order(Parcel source) { ID = source.readInt(); OrderID = source.readInt(); CustomerID = source.readInt(); TotalAmount = source.readDouble(); TotalProducts = source.readDouble(); OrderDate = source.readString(); InvoiceDate = source.readString(); DeliveryDate = source.readString(); // Custom objects Customer = source.readParcelable(Customer.class.getClassLoader()); History = new ArrayList<OrderHistory>(); source.readTypedList(History, OrderHistory.CREATOR); State = source.readParcelable(OrderState.class.getClassLoader());}public int describeContents() { return Order.class.hashCode();}public voID writetoParcel(Parcel dest, int flags) { dest.writeInt(ID); dest.writeInt(OrderID); dest.writeInt(CustomerID); dest.writeDouble(TotalAmount); dest.writeDouble(TotalProducts); dest.writeString(OrderDate); dest.writeString(InvoiceDate); dest.writeString(DeliveryDate); // Custom object dest.writeParcelable(Customer, flags); dest.writeParcelable(State, flags); dest.writeTypedList(History);}
编辑:为OrderList类添加了代码:
public class OrderList extends ArrayList实现Parcelable {
/** * */private static final long serialVersionUID = 417326483896340670L;public OrderList() {}public OrderList(Parcel in) { readFromParcel(in);}/* * Parcelabe interface implementation */public OrderList(Collection<Order> values) { this.addAll(values);}@SuppressWarnings("unchecked")public static final Parcelable.Creator<OrderList> CREATOR = new Parcelable.Creator<OrderList>() { public OrderList createFromParcel(Parcel in) { return new OrderList(in); } public OrderList[] newArray(int arg0) { return null; }};private voID readFromParcel(Parcel in) { this.clear(); //First we have to read the List size int size = in.readInt(); //Reading remember that we wrote first the name and later the Phone Number. //Order is fundamental for (int i = 0; i < size; i++) { Order o = new Order(); o = in.readParcelable(Order.class.getClassLoader()); this.add(o); }}public int describeContents() { return 0;}public voID writetoParcel(Parcel dest, int flags) { int size = this.size(); // We have to write the List size, we need him recreating the List dest.writeInt(size); // We decIDed arbitrarily to write first the name and later the Phone Number. for (int i = 0; i < size; i++) { Order o = this.get(i); dest.writeParcelable(o, flags); }}
}
有什么指针吗?
如果您需要,请随时询问具体信息!
解决方法:
问题似乎是由Order类的两个Integer属性引起的.其中一个从未初始化,因此为null.将其写入Parcel时,它会导致整个函数失败,并导致在结果列表中d出空值.奇怪的是,这不会引发任何异常.
将我的Integer属性更改回int修复了问题(因为int永远不为null).由于我确实需要维护Integer类型,因此我通过将parcel中的空值作为值-1来修复它.
这归结为:
public class Order implements Parcelable { public Integer ID; public Integer OrderID; public Order(Parcel source) { ID = source.readInt(); if (ID.intValue() == -1) { ID = null; } OrderID = source.readInt(); if (OrderID.intValue() == -1) { OrderID = null; } } public voID writetoParcel(Parcel dest, int flags) { if (ID == null) { dest.writeInt(-1); } else { dest.writeInt(ID); } if (OrderID == null) { dest.writeInt(-1); } else { dest.writeInt(OrderID); } }}
总结 以上是内存溢出为你收集整理的android – 通过IPC将对象列表从服务传递到活动时,Parcelable项目消失了全部内容,希望文章能够帮你解决android – 通过IPC将对象列表从服务传递到活动时,Parcelable项目消失了所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)