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. 记录TortoiseGit=>https请求/ssh请求配置

    ssh C:\Program Files\Git\usr\bin\ssh.exe https C:\Program Files\TortoiseGit\bin\TortoisePlink.exe

  2. linux scp命令 将数据从一台linux服务器复制到另一台linux服务器

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...

  3. BayaiM__ORACLE之ASM概念 --V 1.0.0

    BayaiM__ORACLE之ASM概念                                --V 1.0.0 -------------------------------------- ...

  4. 大话IdentityServer4之使用 IdentityServer4 保护 ASP.NET Core 应用

    这几天一直在研究IdentityServer4在asp.net core3.0中的应用,下面说说我的理解: 我们每一个.net core 项目大家可以理解为我新建了一个动物园或者植物园等,注册用户想要 ...

  5. appium---元素定位方法

    在我们做自动化测试的过程中,最基本的就是要会元素定位,也是自动化中的灵魂所在,如果一个自动化测试工程师说不会定位元素定位,那么肯定也不会做自动化了. 如何查看元素 小伙伴们都知道如果是web端可以通过 ...

  6. 【cf995】F. Cowmpany Cowmpensation(拉格朗日插值)

    传送门 题意: 给出一颗树,每个结点有取值范围\([1,D]\). 现在有限制条件:对于一个子树,根节点的取值要大于等于子数内各结点的取值. 问有多少种取值方案. 思路: 手画一下发现,对于一颗大小为 ...

  7. 浅谈js的事件冒泡和事件捕获

    本文地址:https://www.cnblogs.com/christineqing/p/7607113.html 前言:    这篇文章起源于上次工作上的原因,在事件上出的bug,所以就抽空写出一篇 ...

  8. python官方库安装包大全

    https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely 需要什么包就 ctrl+f 搜索对应包 然后使用 pip install xxx\xxx\xxx ...

  9. html各种弹出框和提示框

    控制台输出 console.log() console.info() confirm() alert() promt()   提示对话框

  10. SecureCRT 8.1工具下载和破解附Xshell6

    附教程:https://jingyan.baidu.com/article/eae078275917861fec548592.html 这一篇教程实际上已经说得非常明确了,只需要把注册机放在和secu ...