择忆霖心 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 关于c++外部库文件宏的问题,求解
  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误