温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • IntentService中使用Toask报错sending message to a Handler on a dead thread

IntentService中使用Toask报错sending message to a Handler on a dead thread

发布时间:2020-07-27 13:39:21 来源:网络 阅读:2558 作者:袁斟 栏目:开发技术

在自己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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI