致命异常:java.lang.IllegalStateException:在Scheduler.Worker线程上抛出致命异常

时间:2016-07-26 14:36:21

标签: android multithreading retrofit rx-java rx-android

我在使用改造进行网络通话时遇到了崩溃。这是我的堆栈跟踪。

    Fatal Exception: java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5584)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by rx.b.i: Error occurred when trying to propagate error to Observer.onError
       at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:2192)
       at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71)
       at rx.observers.SerializedObserver.onError(SerializedObserver.java:158)
       at rx.observers.SerializedSubscriber.onError(SerializedSubscriber.java:79)
       at rx.internal.operators.OperatorTakeUntil$1.onError(OperatorTakeUntil.java:49)
       at rx.internal.operators.OperatorToObservableList$1.onError(OperatorToObservableList.java:99)
       at rx.internal.operators.OperatorFilter$1.onError(OperatorFilter.java:47)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:243)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:779)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:540)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:529)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onError(OperatorMerge.java:253)
       at rx.internal.operators.OperatorMap$1.onError(OperatorMap.java:48)
       at rx.internal.operators.OnSubscribeRedo$4$1.onError(OnSubscribeRedo.java:331)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:243)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:779)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:540)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:529)
       at rx.internal.operators.OperatorMerge$InnerSubscriber.onError(OperatorMerge.java:813)
       at rx.Observable$ThrowObservable$1.call(Observable.java:11200)
       at rx.Observable.unsafeSubscribe(Observable.java:8314)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:7235)
       at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
       at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
       at rx.internal.operators.OnSubscribeRedo$3$1.onNext(OnSubscribeRedo.java:1307)
       at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150)
       at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext$a2d69e2(SubjectSubscriptionManager.java:253)
       at rx.subjects.BehaviorSubject.onNext(BehaviorSubject.java:160)
       at rx.internal.operators.OnSubscribeRedo$2$1.onError(OnSubscribeRedo.java:242)
       at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:254)
       at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:186)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5584)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by rx.b.a: 2 exceptions occurred. 
       at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:2192)
       at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71)
       at rx.observers.SerializedObserver.onError(SerializedObserver.java:158)
       at rx.observers.SerializedSubscriber.onError(SerializedSubscriber.java:79)
       at rx.internal.operators.OperatorTakeUntil$1.onError(OperatorTakeUntil.java:49)
       at rx.internal.operators.OperatorToObservableList$1.onError(OperatorToObservableList.java:99)
       at rx.internal.operators.OperatorFilter$1.onError(OperatorFilter.java:47)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:243)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:779)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:540)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:529)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onError(OperatorMerge.java:253)
       at rx.internal.operators.OperatorMap$1.onError(OperatorMap.java:48)
       at rx.internal.operators.OnSubscribeRedo$4$1.onError(OnSubscribeRedo.java:331)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:243)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:779)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:540)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:529)
       at rx.internal.operators.OperatorMerge$InnerSubscriber.onError(OperatorMerge.java:813)
       at rx.Observable$ThrowObservable$1.call(Observable.java:11200)
       at rx.Observable.unsafeSubscribe(Observable.java:8314)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:7235)
       at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
       at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
       at rx.internal.operators.OnSubscribeRedo$3$1.onNext(OnSubscribeRedo.java:1307)
       at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150)
       at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext$a2d69e2(SubjectSubscriptionManager.java:253)
       at rx.subjects.BehaviorSubject.onNext(BehaviorSubject.java:160)
       at rx.internal.operators.OnSubscribeRedo$2$1.onError(OnSubscribeRedo.java:242)
       at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:254)
       at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:186)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       at android.os.Handler.handleCallback(Handler.java:733)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5584)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by rx.b.b: Chain of Causes for CompositeException In Order Received =>
       at com.crashlytics.android.core.SessionProtobufHelper.getEventAppExecutionExceptionSize(SessionProtobufHelper.java:619)
       at com.crashlytics.android.core.SessionProtobufHelper.getEventAppExecutionExceptionSize(SessionProtobufHelper.java:622)
       at com.crashlytics.android.core.SessionProtobufHelper.getEventAppExecutionExceptionSize(SessionProtobufHelper.java:622)
       at com.crashlytics.android.core.SessionProtobufHelper.getEventAppExecutionSize$322e56cf(SessionProtobufHelper.java:554)
       at com.crashlytics.android.core.SessionProtobufHelper.getEventAppSize$565823c3(SessionProtobufHelper.java:510)
       at com.crashlytics.android.core.SessionProtobufHelper.getSessionEventSize(SessionProtobufHelper.java:2482)
       at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.writeSessionEvent$13f1f7ff(CrashlyticsUncaughtExceptionHandler.java:1052)
       at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.writeFatal(CrashlyticsUncaughtExceptionHandler.java:23766)
       at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler$5.call(CrashlyticsUncaughtExceptionHandler.java:1238)
       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 io.fabric.sdk.android.services.common.ExecutorUtils$1$1.onRun(ExecutorUtils.java:75)
       at io.fabric.sdk.android.services.common.BackgroundPriorityRunnable.run(BackgroundPriorityRunnable.java:30)
       at java.lang.Thread.run(Thread.java:841)
Caused by retrofit.RetrofitError
       at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
       at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
       at $Proxy4.getChartStockData(Unknown Source)
       at com.talkoot.android.web.GoogleFinanceManager.access$lambda$0(GoogleFinanceManager.java:1082)
       at com.talkoot.android.web.GoogleFinanceManager$$Lambda$1.call(Unknown Source)
       at rx.internal.operators.OnSubscribeFromCallable.call(OnSubscribeFromCallable.java:1032)
       at rx.Observable$2.call(Observable.java:1162)
       at rx.Observable.unsafeSubscribe(Observable.java:8314)
       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       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.access$201(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:841)
Caused by java.net.SocketTimeoutException
       at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
       at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
       at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
       at java.io.InputStream.read(InputStream.java:162)
       at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
       at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
       at com.android.okhttp.internal.Util.readAsciiLine(Util.java:316)
       at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
       at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
       at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
       at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
       at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
       at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
       at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
       at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
       at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
       at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
       at $Proxy4.getChartStockData(Unknown Source)
       at com.talkoot.android.web.GoogleFinanceManager.access$lambda$0(GoogleFinanceManager.java:1082)
       at com.talkoot.android.web.GoogleFinanceManager$$Lambda$1.call(Unknown Source)
       at rx.internal.operators.OnSubscribeFromCallable.call(OnSubscribeFromCallable.java:1032)
       at rx.Observable$2.call(Observable.java:1162)
       at rx.Observable.unsafeSubscribe(Observable.java:8314)
       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       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.access$201(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:841)

这是我的代码。

 public void downloadChartFullData(@NonNull String ticker, @NonNull String exchange, @NonNull ChartTimePeriod chartTimePeriod,
                                      @NonNull ChartFragment fragment) {
        if (!ConnectionUtils.isNetworkConnected(false))
            return;
        Log.v(TAG + Utils.getCurrentThreadInfo(), "handleActionDownloadChartStockData");
        getChartStockData(ticker, exchange)
                .compose(fragment.bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(RetryWithDelay.newInstance())
                .flatMap(new Func1<List<ChartDataEntity>, Observable<ChartDataEntity>>() {
                    @Override
                    public Observable<ChartDataEntity> call(List<ChartDataEntity> stockEntities) {
                        return Observable.from(stockEntities);
                    }
                })
                .filter(new Func1<ChartDataEntity, Boolean>() {
                    @Override
                    public Boolean call(ChartDataEntity ChartDataEntity) {
                        return ChartDataEntity.getDate() != null; // to filter out data without Date.
                    }
                })
                .toList()
                .compose(fragment.bindToLifecycle())
                .doOnNext(new Action1<List<ChartDataEntity>>() {
                    @Override
                    public void call(List<ChartDataEntity> stockEntities) {
                        mBus.post(new OnChartDataReceivedEvent(TAG, stockEntities, ticker, exchange,
                                chartTimePeriod));
                    }
                })
                .subscribe(new Action1<List<ChartDataEntity>>() {
                    @Override
                    public void call(List<ChartDataEntity> stockEntities) {
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e("TAG", throwable.getMessage());
                    }
                });
    }

public Observable<List<ChartDataEntity>> getChartStockData(@NonNull String ticker, @NonNull String exchange) {
        return Observable.fromCallable(() -> {
            Log.i(TAG + Utils.getCurrentThreadInfo(), "Getting stockChart");
            Response response = mGoogleFinanceWebInterface.getChartStockData(ticker, exchange,
                    GoogleFinanceWebInterface.INTERVAL_DEFAULT, GoogleFinanceWebInterface.PERIOD_DEFAULT,
                    new GoogleFinanceWebInterface.ChartParameters());
            String strResponse = responseMapper.call(response);
            return StockUtils.convertResponseToChartData(strResponse);
        });
    }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您似乎需要在第二个 撰写 操作符之前启动 mainThread ,因为改造相关的执行应该在同一个线程上。

所以添加这一行

.observeOn(AndroidSchedulers.mainThread())

toList()

之后