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();
  }
});

 

原文地址:https://www.cnblogs.com/qikeyishu/p/9151179.html