Can't create handler inside thread that has not called Looper.prepare()

来源:互联网 发布:视频剪辑用什么软件 编辑:程序博客网 时间:2024/05/08 08:44

简单说在非主线程内调用handler去接收消息,频繁的话就会出现这个错误,意思是缺少Looper,那么怎么解决呢?

很幸运找到一个解决办法的博客,作者昵称sonicit,具体解决办法如下:


this.mThirdHandler = new Handler(mContext.getMainLooper()){            @Override            public void handleMessage(android.os.Message msg) {                           };        };
在new Handler的时候可以指定Looper故给非主线程的Handler实例化的时候需要
new Handler(mContext.getMainLooper())指定主线程的Looper即可解决问题。
解释一下,一个主进程只能有一个Looper所以需要指定用主进程的Looper。

0 0
原创粉丝点击