转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代码如下: public class MainActivity extends Activity implements OnClickListener { private static final int UPDATE_TEXT=1; private TextView textView; privat…
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要改动 ui 仅仅能 post 到主线程或者使用 handler 之类.可是细致看看exception的描写叙述并非这种."Only the original thread that created a view hierarchy can touch its views".仅仅有创建该 v…
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 这是因为Handler对象与其调用者在同一线程中,如果在Handler中设置了延时操作,则调用线程也会堵塞.每个Handler对象都会绑定一个Looper对象,每个Looper对象对应一个消息队列(MessageQueue).如果在创建Ha…
今天在做练习时,在一个新开启的线程中调用“Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();” 报错为:Can't create handler inside thread that has not called Looper.prepare() 在新线程中添加Looper.prepare();和Looper.loop();即可. 示例代码段:(该代码在新开的线程中) Looper.prepare()…