2017-04-20 53 views
2

我在我的應用程序中使用了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))); 
+0

你正在測試什麼模擬器版本? – yosriz

+0

@yosriz API 25(nexus 5x) –

回答

4

的問題是,你使用的是Java 8的Stream API低於24

目前,它的API級別僅支持API級別24。

當Google放棄Jack和Jill的新工具鏈時,Java 8的功能支持機制最近發生了變化,而支持使用舊工具鏈的Java 8功能的lately announced的某些調用「desugar」將把Java8類文件轉換爲Java 7兼容的類文件(可從Android Studio 2.4 Preview 6中獲得)。
您可以閱讀更多here

+0

我很想。謝謝,這麼多機會,但我們不能使用它們。可悲的是。 –

+2

還有很多選擇,你可以搜索流api android – yosriz

1

謝謝yosriz。 我使用:https://github.com/aNNiMON/Lightweight-Stream-API。 我的代碼一直在尋找這樣的:

couponsCode.forEach(value -> couponsCodes.add(new CouponsCode(value))); 

而且我的代碼看起來像現在:

Stream.of(couponsCode).forEach(value -> couponsCodes.add(new CouponsCode(value))); 

所有的錯誤都沒有了。