RxJava序列不包含任何元素错误

时间:2017-06-21 09:53:56

标签: android rx-java

我正在使用RxJava并且一切看起来都不错,除非在一种情况下会抛出这些错误。

 Caused by: java.util.NoSuchElementException: Sequence contains no elements
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:118)
at rx.internal.operators.OperatorTake$1.onCompleted(OperatorTake.java:56)
at rx.internal.operators.OnSubscribeFilter$FilterSubscriber.onCompleted(OnSubscribeFilter.java:99)
at rx.internal.operators.OnSubscribeFromIterable$IterableProducer.slowpath(OnSubscribeFromIterable.java:134)
at rx.internal.operators.OnSubscribeFromIterable$IterableProducer.request(OnSubscribeFromIterable.java:89)
at rx.internal.operators.OperatorTake$1$1.request(OperatorTake.java:109)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OperatorTake$1.setProducer(OperatorTake.java:93)
at rx.internal.operators.OnSubscribeFilter$FilterSubscriber.setProducer(OnSubscribeFilter.java:104)
at rx.internal.operators.OnSubscribeFromIterable.call(OnSubscribeFromIterable.java:63)
at rx.internal.operators.OnSubscribeFromIterable.call(OnSubscribeFromIterable.java:34)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OnSubscribeFilter.call(OnSubscribeFilter.java:45)
at rx.internal.operators.OnSubscribeFilter.call(OnSubscribeFilter.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:250)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:147)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emitScalar(OperatorMerge.java:399)
at rx.internal.operators.OperatorMerge$MergeSubscriber.tryEmit(OperatorMerge.java:357)
at rx.internal.operators.OperatorMerge$InnerSubscriber.onNext(OperatorMerge.java:852)
at rx.internal.operato

编辑: 问题出在函数NumberObservables::mergeNumberAndName

public static Observable<Trailer> mergeTrailerType(Trailer trailer) {
    String id = number.getId();
    return Observable.from(Store.names())
            .first(name -> id.equals(name.getId()))
            .map(name -> {
                number.setName(name);
                return number;
            });
}

我可以看到first出现问题,但我不能用take(1)或firstOrDefault(找不到getId())替换它。可以使用single吗?

1 个答案:

答案 0 :(得分:1)

您可能会在流NumberObservables::getByNumberNumberObservables::mergeNumberAndNamesingle()运算符的某处应用,可以在堆栈跟踪处观察到:

at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:118)

single()期望源Observable只发出一个项目,如果源Observable没有从堆栈跟踪中发出任何项目,则通知NoSuchElementException错误,这可能是由于某些过滤器所致你申请了:

at rx.internal.operators.OnSubscribeFilter$FilterSubscriber.onCompleted(OnSubscribeFilter.java:99)