择忆霖心 2017-03-27 10:33 采纳率: 96.7%
浏览 859
已采纳

求大神看看代码,帮帮我

public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { try { String username = usernameEdit.getText().toString(); String password = passwordEdit.getText().toString(); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("username", username) .add("password", password) .build(); Request request = new Request.Builder() .url("http://test.ourwechat.com/test/auth.php") .post(requestBody) .build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); JSONObject object = new JSONObject(responseData); code = object.getInt("code"); if (code ==-1){ Toast.makeText(Login.this,"登录失败",Toast.LENGTH_SHORT).show(); }else if (code == 1){ Intent home = new Intent(Login.this, Homepage.class); startActivity(home); } } catch (JSONException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }).start();
以上是代码
以下是错误日志
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:200)
at android.os.Handler.(Handler.java:114)
at android.widget.Toast$TN.(Toast.java:348)
at android.widget.Toast.(Toast.java:101)
at android.widget.Toast.makeText(Toast.java:260)
at com.money.Login.Login$1$1.run(Login.java:90)
at java.lang.Thread.run(Thread.java:761)

求大神指出问题

  • 写回答

3条回答

  • stone_you 2017-03-28 01:56
    关注

    不要在子线程中使用toast

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示