我正在编写一个运行非本地服务的 Android 应用程序,以便从 Internet 获取数据。我有一个绑定(bind)到所述服务并检索订单对象列表以显示它们的 Activity 。
将列表从服务传递到 Activity 时似乎出了点问题。虽然这对我来说似乎很简单。
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 是绑定(bind)到服务的结果):
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);
}
}
});
这让我完全被难住了,因为我还有一个查看客户的 Activity 和一个返回客户列表的服务功能,而且运行完美。两者之间的主要区别在于 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)类 OrderList 扩展 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 属性引起的。其中之一从未被初始化,因此为空。将其写入 Parcel 时,它失败导致整个函数失败,并导致空值在结果列表中弹出。奇怪的是这不会抛出任何异常。
将我的 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 将对象列表从服务传递到 Activity 时,Parcelable 项目消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191657/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo