2016-10-03 98 views
0

我嘗試一些代碼,這裏是例外,我得到:在Scheduler.Worker線程上拋出異常。加上`onError`處理

Process: in.ddas.pretense, PID: 8249 
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling. 
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60) 
    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:818) 
Caused by: rx.exceptions.OnErrorNotImplementedException: Can't create handler inside thread that has not called Looper.prepare() 
    at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386) 
    at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383) 
    at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44) 
    at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157) 
    at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120) 
    at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:204) 
    at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:144) 
    at rx.internal.operators.OperatorSubscribeOn$1$1.onNext(OperatorSubscribeOn.java:53) 
    at rx.internal.operators.OperatorTake$1.onNext(OperatorTake.java:77) 
    at rx.internal.operators.OnSubscribeTimerPeriodically$1.call(OnSubscribeTimerPeriodically.java:52) 
    at rx.Scheduler$Worker$1.call(Scheduler.java:134) 
    at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:187) 
    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:818)  
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
    at android.os.Handler.<init>(Handler.java:200) 
    at android.os.Handler.<init>(Handler.java:114) 
    at in.ddas.pretense.GoogleCloudActivity.PredictionEngine(GoogleCloudActivity.java:198) 
    at in.ddas.pretense.GoogleCloudActivity.lambda$onClick$0(GoogleCloudActivity.java:176) 
    at in.ddas.pretense.GoogleCloudActivity$$Lambda$1.call(Unknown Source) 
    at rx.internal.util.ActionSubscriber.onNext(ActionSubscriber.java:39) 
    at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139) 
    at rx.internal.operators.OperatorSubscribeOn$1$1.onNext(OperatorSubscribeOn.java:53)  
    at rx.internal.operators.OperatorTake$1.onNext(OperatorTake.java:77)  
    at rx.internal.operators.OnSubscribeTimerPeriodically$1.call(OnSubscribeTimerPeriodically.java:52)  
    at rx.Scheduler$Worker$1.call(Scheduler.java:134)  
    at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$2.call(EventLoopsScheduler.java:187)  
    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:818)  

下面是相同的代碼:

try { 
        kf.initFilter(); 
        kg.initFilter(); 
        Observable.interval(1, TimeUnit.SECONDS) 
          .take(5) 
          // switch execution into main thread 
          .subscribeOn(AndroidSchedulers.mainThread()) 
          .subscribe(t -> { 
           PredictionEngine(1); 
          }); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

private void PredictionEngine(int delay) throws Exception { 

    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      enableStrictMode(); 
      String val = null; 
      try { 
       if (tHighPass == 0 && tLowPass == 0 && tKalman == 1) { 
        //Magic 
       } else { 
        //Magic 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      enableStrictMode(); 
      side_output.append(val + "\n"); 
      new DropboxTask(side_output, "Result", val).execute(); 
     } 
    }, delay); 
} 

我基本上是試圖循環運行函數在一定的時間內不阻塞主線程,具有一定的延遲。

我在哪裏做錯了?

謝謝!

+0

閱讀不僅僅是堆棧跟蹤的第一行更加它有時重要的實現Looper.prepare()' –

+0

@TylerSebastian我是不是在已經使用'AndroidSchedulers.mainThread()'的主線程上運行我的函數? –

+0

默認構造函數使用* current *線程的循環。你應該使用'Hander h = new Handler(Looper.getMainLooper());' –

回答

1

Handler的默認構造函數使用Looper實例爲當前線程。如果您打算在主線程上運行您的代碼,並且您的處理程序正在主程序以外的線程中初始化,則需要指定Looper以供處理程序使用。了java.lang.RuntimeException:無法內螺紋創建處理程序已經不叫`所致 - 這可以通過使用

Handler h = new Handler(Looper.getMainLooper());

+1

P.S.喜歡你設計你的網站的方式:) –

+1

@ Jeet.Deir謝謝我絕不是一個設計師,我認爲這很聰明。 –