2013-10-02 92 views
3

我正在用這個甩掉我的頭髮!我是Android的新手,所以我相信這是非常明顯的。Android ScheduledThreadPoolExecutor原因:null

我得到一個ScheduledThreadPoolExecutor例外,cause: null

我要的是運行時的活動是在屏幕上一個單獨的線程!

// Instance Variables 
private ScheduledExecutorService m_oScheduledExecutor = null; 

@Override 
protected void onResume() 
{ 
super.onResume(); 

if (oScheduledExecutor == null) 
{ 
    oScheduledExecutor = Executors.newSingleThreadScheduledExecutor(); 
} 

    try 
    { 
     oScheduledExecutor.scheduleAtFixedRate({Runnable Instance HERE}, 0, 10, TimeUnit.SECONDS); 
    } 
    catch (Exception e) 
    { 
     System.out.println("(MainActivity) Error: " + e.getMessage() + " Cause: " + e.getCause()); 
    } 
} 

@Override 
protected void onStop() 
{ 
    super.onStop(); 
    m_oScheduledExecutor.shutdown(); 
} 

編輯:整個堆棧跟蹤....

java.util.concurrent.RejectedExecutionException: Task java.[email protected]41976688 rejected from [email protected][Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1] 
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1979) 
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:786) 
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:300) 
at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:545) 
at java.util.concurrent.Executors$DelegatedScheduledExecutorService.scheduleAtFixedRate(Executors.java:619) 
at com.example.wifitest.MainActivity.onResume(MainActivity.java:61) 
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185) 
at android.app.Activity.performResume(Activity.java:5182) 
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732) 
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5041) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
at dalvik.system.NativeStart.main(Native Method) 
+0

請發佈整個堆棧跟蹤。 – CommonsWare

回答

8

你不能 '回收' 的ExecutorService。一旦你調用了shutdown(),試圖安排任何任務將導致拒絕,並在你的情況下拒絕政策是拋出RejectedExecutionException

如果您按照您的堆棧跟蹤,你可以在ScheduledThreadPoolExecutor看到:

/** 
* Specialized variant of ThreadPoolExecutor.execute for delayed tasks. 
*/ 
private void delayedExecute(Runnable command) { 
    if (isShutdown()) { 
     reject(command); 
     return; 
    } 
    // ... 
} 

保持你的遺囑執行人的服務作爲一個實例變量是不會爲你在這裏工作:一旦它被關閉,就不能再次使用。

0

不要在onStop()方法內關閉ScheduledExecutorService。嘗試把它放在onDestroy()方法中。當您的活動在後臺進行時,onStop()方法可能會被調用,因爲您的活動在後臺不可見。因此,如果ScheduledExecutorService被關閉,您可能會收到此錯誤。

相關問題