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

FATAL EXCEPTION: OkHttp Dispatcher

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

产生异常的原因:只是想写个Toast.makeText(MainActivity.this,"连接成功!",Toast.LENGTH_SHORT).show();来提示连接服务器成功或者失败,为什么会出错。实际需要调用线程来启动Toast,不能直接调用。

解决方法:调用线程来处理

runOnUiThread(new Runnable() {
  @Override
  public void run() {
  Toast.makeText(MainActivity.this,"连接成功!",Toast.LENGTH_SHORT).show();
  }
});

 

posted @ 2018-06-07 16:07  牛新龙的IT技术博客  阅读(627)  评论(0编辑  收藏  举报