您好,登錄后才能下訂單哦!
在自己IntentSevice繼承類中的onHandleIntent方法中寫Toast.makeText(getApplicationContext(), "sd不存在", Toast.LENGTH_SHORT).show();時不會有任何提示,logcat中提示“sending message to a Handler on a dead thread”錯誤。后從網上差報錯原因了解到當一個線程的消息循環已經退出后,不能再給其發送消息不如就會報錯。后有從一個網站找到了解決方案,和產生錯誤的原因。
The problem here is that you are creating a Toast inside a thread that is managed by the IntentService. The system will use the Handler associated with this thread to show and hide the Toast.
First the Toast will be shown correctly, but when the system tries to hide it, after the onHandleIntent method has finished, the error "sending message to a Handler on a dead thread" will be thrown because the thread on which the Toast was created is no longer valid, and the Toast will not disappear.
這里的問題是,你正在創建一個由IntentService管理的Toast內部線程。該系統將使用與此線程關聯的處理程序,以顯示和隱藏Toast。首先,Toast將正確顯示出來,但是當系統試圖將其隱藏時onHandleIntent方法已經完成,“sending message to a Handler on a dead thread”的錯誤將被拋出,因為Toast在其上創建的線程不再有效,且Toast不會消失。
解決方法:
// create a handler to post messages to the main thread Handler mHandler = new Handler(getMainLooper()); mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show(); } });
引用:http://www.xuebuyuan.com/36739.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。