获取整数列表的平均值

Get the average of a List of integers

我刚开始使用 RxJava,这是我的代码:

Observable<Integer> averageObservable = 
MathObservable.averageInteger(Observable.just(11, 2, 3, 4, 5));

这是我的错误:

java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
            at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:152)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NoSuchMethodError: No static method identity()Lrx/functions/Func1; in class Lrx/functions/Functions; or its super classes (declaration of 'rx.functions.Functions' appears in /data/app/de.asideas.images-2/base.apk)
            at rx.observables.MathObservable.averageInteger(MathObservable.java:89)
            at de.asideas.images.ui.GitHubChallengeFragment.call(GitHubChallengeFragment.java:81)
            at de.asideas.images.ui.GitHubChallengeFragment.call(GitHubChallengeFragment.java:61)
            at rx.internal.operators.OperatorMap.onNext(OperatorMap.java:55)
            at rx.internal.operators.OperatorMerge$InnerSubscriber.emit(OperatorMerge.java:676)
            at rx.internal.operators.OperatorMerge$InnerSubscriber.onNext(OperatorMerge.java:586)
            at retrofit.RxSupport.run(RxSupport.java:56)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at retrofit.Platform$Android.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: java.util.ArrayList.class
            at rx.internal.operators.OperatorMerge$InnerSubscriber.emit(OperatorMerge.java:679)
            at rx.internal.operators.OperatorMerge$InnerSubscriber.onNext(OperatorMerge.java:586)
            at retrofit.RxSupport.run(RxSupport.java:56)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at retrofit.Platform$Android.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:818)

您需要订阅您的观察员。

//create the observable
Observable<Integer> averageObservable = MathObservable.averageInteger(Observable.just(11, 2, 3, 4, 5));

//subscribe to the observer 
averageObservable.subscribe(new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            Log.d(TAG, "call " + integer);
            //Result = 5
        }
    });

我测试了代码没有问题,如果你有问题告诉我:)