避免NetworkOnMainThreadException [英] avoiding NetworkOnMainThreadException

查看:198
本文介绍了避免NetworkOnMainThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经理解的是,未在GUI线程上允许的网络操作。好吧我。但是,为什么在一个对话框按钮单击回调使用此code仍然产生 NetworkOnMainThreadException

I've understood that a network operation is not allowed on the GUI thread. Ok for me. But why using this code on a Dialog button click callback still produce NetworkOnMainThreadException ?

new Thread(new Runnable() {                         
  @Override
  public void run() {
    heavyAndTimeConsumingOperation();
  }
}).run();

也许我不会生成一个新的线程这样?

perhaps i'm not spawning a new thread this way?

推荐答案

您需要调用。开始(),不是 .RUN()

.RUN()是您覆盖实际执行的方法。
。开始()是实际启动一个线程系统提供的方法。

.run() is the method you override that actually executes.
.start() is a system-provided method that actually starts a thread.

这篇关于避免NetworkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆