我在我的應用程序中使用了rxJava。我經常使用這樣的代碼:通過rxJava函數中的foreach導致Android設備崩潰
couponListInteractor.getObservableData(storeId)
.subscribe(data ->
data.forEach(item ->
{
if (!couponsCode.contains(item.getCode()))
{
couponsCode.add(item.getCode());
}
}
);
當我在我的模擬器中開發應用程序時,everething沒問題。但是當我開始測試我的應用程序在設備(4.0安卓版)。我收到一些錯誤。
FATAL EXCEPTION: RxNewThreadScheduler-1
Process: com.mrrebates.cashback, PID: 20428
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoClassDefFoundError: com.couponautoapplier.cashback.model.api.apistorage.DataConfigApiStorage$1$$Lambda$1
at com.couponautoapplier.cashback.model.api.apistorage.DataConfigApiStorage$1.call(DataConfigApiStorage.java:50)
at com.couponautoapplier.cashback.model.api.apistorage.DataConfigApiStorage$1.call(DataConfigApiStorage.java:28)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:41)
at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:38)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.Subscriber.setProducer(Subscriber.java:205)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10200)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10200)
at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
我容易糾正這個錯誤:
couponListInteractor.getObservableData(storeId)
.subscribe(data -> {
for(CouponListItemViewModel item: data)
{
if(!couponsCode.contains(item.getCode()))
{
couponsCode.add(item.getCode());
}
}
});
我必須糾正的foreach處處爲?
我不想這樣做,因爲它看上去很美:
couponsCode.forEach(value -> couponsCodes.add(new CouponsCode(value)));
你正在測試什麼模擬器版本? – yosriz
@yosriz API 25(nexus 5x) –