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 ...
随机推荐
- 软工个人项目(Java实现)
一. Github地址: https://github.com/RuiBingo/PersonalWork 二.个人PSP表格: PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟) Plan ...
- SpringBoot+Thyemelaf开发环境正常,打包jar发到服务器就报错Template might not exist or might not be accessible
这里说一下Thyemelaf的巨坑 写了一个SpringBoot+Thyemelaf的项目,并不是前后端分离.今天想放到linux服务器上玩玩,打成jar包,然后一运行他妈居然报错了,报了一个Temp ...
- [b0019] python 归纳 (五)_类装饰器
总结: 类装饰器, 本质是一个函数,输入一个类,返回一个类 Case 1 啥都没做 def deco(in_class): return in_class @deco class Cat: def _ ...
- python中的随机模块random
random模块是 python 中为随机数所使用的模块 ```import random # 随机生成0-1范围内的随机浮点数i = random.random()print(i) # 随机生成范围 ...
- 源码编译Kubeadm二进制文件
kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,伴随Kubernetes每个版本的发布都会同步更新,kubeadm会对集群配置方面的一些实践做调整,通过实验k ...
- c# 第41节 异常处理
本节内容: 1:捕获异常两种方式 2:处理异常的两种方式 3:实例处理异常 4:手动触发异常 5:预定义异常类有哪些 6:自定义异常 1:捕获异常 第一种 捕获:只能知道发生了异常 第二种捕获: 可以 ...
- 遗传算法求解旅行商(TSP)问题 -- python
参考资料: 遗传算法解决TSP旅行商问题(附:Python实现) 遗传算法详解(GA)(个人觉得很形象,很适合初学者) from itertools import permutations impor ...
- 【Spring JDBC】JdbcTemplate(三)
传统Jdbc API与Spring jdbcTemplate比较 //JDBC API Statement statement = conn.createStatement(); ResultSet ...
- Note | MATLAB
目录 1. 读写文件 简单读写 将rgb保存为yuv文件 从yuv文件中读取Y通道 将TIFF图片拼接为yuv文件 2. 字符串操作 3. 画图 4. 词频统计 1. 读写文件 简单读写 fp = f ...
- ES6中有关数组的一些新操作
1.Array.isArray() 用于确定传递的值是否是一个 Array. Array.isArray([1, 2, 3]); // true Array.isArray({foo: 123}); ...