Android开发中Can't toast on a thread that has not called Looper.prepare()问题

说一下问题出现场景:

  在一个Android项目中,利用okhttp进行网络访问判断用户输入的账号密码,当用户密码输错时弹出 Toast 进行提示。

截取部分代码如下:

利用okhttp进行网络访问代码(其中User类是用来包装用户名和密码):

import com.hzau.xiaonongfu.Entity.User;
import okhttp3.*; public class HttpUtil {
private static final String TAG="HttpUtil"; private static final OkHttpClient client=new OkHttpClient().newBuilder().build(); //用户登录验证
public static void login(User user,okhttp3.Callback callback){
//建立表单,添加需要上传到服务器的参数
RequestBody body=new FormBody.Builder()
.add("username",user.getUser_name())
.add("password",user.getPassword())
.build(); //发起请求
Request request=new Request.Builder()
.url(Address.LOGIN_ADDRESS)
.post(body)
.build(); //启动连接,enqueue()会自动开启子线程
client.newCall(request).enqueue(callback);
}
}

在Activity中获取返回数据:

HttpUtil.login(user,new okhttp3.Callback(){
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
String s= Objects.requireNonNull(response.body()).string();
//判断是否登录成功
if(s.equals("0")){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}else if(s.equals("1")){
Toast.makeText(LoginActivity.this,"密码错误,请检查您的密码!",Toast.LENGTH_SHORT).show();
}
} @Override
public void onFailure(@NotNull Call call, @NotNull IOException e) { }
});

这里服务器端采用php脚本的方式,账号密码正确就返回 0 ,错误就返回 1 。

百度了一下,说一下错误的原因吧:

  okhttp中 Callback 是封装好的在子线程运行,Android是不允许在子线程中弹出 Toast 提示的,和不允许在子线程中进行 UI 操作一样。

解决办法:

在 Toast 前后加上一些代码,如下:

Looper.prepare();
Toast.makeText(LoginActivity.this,"密码错误,请检查您的密码!",Toast.LENGTH_SHORT).show();
Looper.loop();

以后有空回来补充其中原理。

吾生也有涯,而知也无涯。

  

Can't toast on a thread that has not called Looper.prepare()的更多相关文章

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

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

  2. 关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法

    形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherD ...

  3. Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误

    原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...

  4. Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()

    MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...

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

    1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...

  6. Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    章出自:luchg技术交流 http://www.luchg.com 版权所有.本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源,谢谢. Android-java.lang.Runti ...

  7. Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()

    Error:Can't create handler inside thread that has not called Looper.prepare() 原代码: //利用Handler消息传递机制 ...

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

    10-12 17:02:55.500: E/AndroidRuntime(28343): FATAL EXCEPTION: Timer-2 10-12 17:02:55.500: E/AndroidR ...

  9. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法

    代码改变世界 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pre ...

随机推荐

  1. SparkStreaming整合flume

    SparkStreaming整合flume 在实际开发中push会丢数据,因为push是由flume将数据发给程序,程序出错,丢失数据.所以不会使用不做讲解,这里讲解poll,拉去flume的数据,保 ...

  2. 获取oracle的建表DLL语句

    get_ddl('TABLE','表名','实例名') from dual   select dbms_metadata.get_ddl('TABLE','RMS_CITY','RMS') from ...

  3. Python—实现ssh客户端(连接远程服务器)

    paramiko是一个基于SSH用于连接远程服务器并执行相关操作(SSHClient和SFTPClinet,即一个是远程连接,一个是上传下载服务),使用该模块可以对远程服务器进行命令或文件操作,值得一 ...

  4. Web服务器—Nginx

    Nginx常用命令: 启动nginx服务 [root@localhost ~]# service nginx start [root@localhost ~]# systemctl start ngi ...

  5. emacs 帮助相关命令

    emacs 帮助相关命令 如下表: No. 键盘操作 键盘操作对应的函数 回答的问题 01 ctrl-h c describe-key-briefly 这个按键组合将运行哪个函数 02 ctrl-h ...

  6. nginx的共享字典项api(操作方法)

    nginx的共享内存,称为共享字典项,对于所有的worker进程都可见,是一种全局变量. 备注一下内容中的 [] 是 备注. 源码分析文档:https://www.codercto.com/a/948 ...

  7. centos下载安装libgcc 和 libtiff

    1. 查看libtiff 可安装列表: [root@e952aff59318 lib]# yum list | grep "libtiff" libtiff.i686 4.0.9- ...

  8. luoguP3017Brownie Slicing

    https://www.luogu.org/problem/P3017 题意 给你一个蛋糕,R行C列 ,每个点有巧克力碎屑(如下) 1 2 2 1 3 1 1 1 2 0 1 3 1 1 1 1 1 ...

  9. c# WF 第1节 创建winform程序

    本节内容: 1:vs的RAD 2:WinForm的创建简介 3:创建窗口与控制台程序文件的对比 4:窗口文件内容 5:winform怎么运行 6:winform的实质 1:vs的RAD 2:WinFo ...

  10. 201871010132-张潇潇-《面向对象程序设计(java)》第八周总结

    201871010132-张潇潇<面向对象程序设计(java)>第八周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这 ...