Can't toast on a thread that has not called Looper.prepare()
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()的更多相关文章
- 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. ...
- 关于子线程使用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 ...
- 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报 ...
- 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 ...
- Can’t create handler inside thread that has not called Looper.prepare()
1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...
- 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 ...
- 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消息传递机制 ...
- 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 ...
- 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 ...
随机推荐
- oracle 死锁 锁
[zhuan]今天看群里在讨论数据库死锁的问题,也一起研究了下,查了些资料在这里总结下. 所谓死锁: 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将 ...
- rabbitmq pika(python)订阅发布多客户端消费场景简单使用
发布端: import pika import time credentials = pika.credentials.PlainCredentials('root', 'root',erase_on ...
- Linux下环境变量(.bash_profile和.bashrc的区别)
在linux系统下,如果下载并安装了应用程序,在启动时很有可能在键入它的名称时出现"command not found"的提示内容.如果每次都到安装目标文件夹内,找到可执行文件来进 ...
- Java 并发(1)——线程安全
我们对并发一词并不陌生,它通常指多个任务同时执行.实际上这不完全对,“并行”才是真正意义上的同时执行,而“并发”则更偏重于多个任务交替执行.有时候我们会看见一些人一边嘴里嚼着东西一边讲话,这是并行:当 ...
- 博客美化——Silence主题皮肤
介绍 一款专注阅读的博客园主题,主要面向于经常混迹 博客园 的朋友.其追求大道至简的终极真理,界面追求简洁.运行追求高效.部署追求简单. 博客皮肤源码地址 预览地址 如何部署.使用皮肤 Silen ...
- itest(爱测试) 4.2.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
itest 入选 2019 年度最受欢迎开源中国软件 开源工具的发展,离不开你我的支持,需要您投上宝贵的一票 去投票 v4.2.0下载地址 :itest下载 itest 简介:查看简介 itest ...
- 线性回归——lasso回归和岭回归(ridge regression)
目录 线性回归--最小二乘 Lasso回归和岭回归 为什么 lasso 更容易使部分权重变为 0 而 ridge 不行? References 线性回归很简单,用线性函数拟合数据,用 mean squ ...
- 23.Java基础_ArrayList类
ArrayList类的构造和使用方法 import java.util.ArrayList; public class test { public static void main(String[] ...
- c# 第40节 密封类、密封方法
本节内容: 1:密封的存在意义 2:密封方法的实现 1:密封的存在意义 2:密封方法的实现 class2::父类 class Class2 { public virtual void show() { ...
- 毕业设计代做,各种系统微服务项目ssm项目,员工管理系统,微信小程序,购物商城,二手商城系统,销售系统,等等
毕业设计代做,各种系统,微服务项目,ssm项目 小程序,商城等,期末作业等都可以,价格好说,长期接单, 有项目说明书,软件介绍相关文档,答辩的时候包过,知识点对接好,给你讲解等, 毕业设计代做,各种系 ...