jenkin_du
2016-04-02 20:23
如果在线程的run方法中直接新建一个继承自Handler类的handler对象,程序会报错误,错误:
Can't create handler inside thread that has not called Looper.prepare()
而将handler作为线程的成员变量则不会报错,为什么
在子线程中new一个Handler的话也可以,但需要在子线程中先调用Looper.prepare()然后是new Handler,最后是Looper.loop(),UI线程中起始自动创建了Looper对象,子线程需要Looper.prepare()来创建;最好看下源码,马上明白。
不能在主线程对UI进行布局,所以要交给Handler
不是很理解你这样的做法,一般的做法是在线程里做耗时操作,然后发送消息给handler去更新UI界面
Android必学-异步加载
50611 学习 · 333 问题