gpt4 book ai didi

java - 由于 rxJava 函数中的 foreach 导致 Android 设备崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:29:11 25 4
gpt4 key购买 nike

我在我的应用程序中使用 rxJava。我经常使用这样的代码:

couponListInteractor.getObservableData(storeId)
.subscribe(data ->
data.forEach(item ->
{
if (!couponsCode.contains(item.getCode()))
{
couponsCode.add(item.getCode());
}
}
);

当我在我的模拟器中开发应用程序时一切正常。但是当我开始在设备(4.0 android 版本)中测试我的应用程序时。我遇到了一些错误。

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 到 for?

我不想这样做,因为它看起来很漂亮:

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

最佳答案

问题是您在 API 级别低于 24 时使用 Java 8 的 Stream API。

目前仅 API 级别 24 支持它。

当谷歌放弃 Jack 和 Jill 的新工具链时,Java 8 特性支持机制最近发生了变化,并且 lately announced使用旧的工具链支持 Java 8 功能,其中包含称为“desugar”的东西,它将 Java8 类文件转换为 Java 7 兼容类文件(可从 Android Studio 2.4 Preview 6 获得)。
您可以阅读更多here .

关于java - 由于 rxJava 函数中的 foreach 导致 Android 设备崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43510109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com