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 ...
 
随机推荐
- BayaiM__linux双网卡绑定文档
			
BayaiM__linux双网卡绑定文档 开门贱山:以下内容纯属原创,如有雷同,爱咋咋滴吧~~!!—————————————————————————————————————————— 1,备份网卡信息 ...
 - 父组件调用子组件中的方法- this.$refs.xxx.子组件方法();
			
子组件中有一个说的方法 在父组件中去调用当你点击的时候 去调用子组件中的方法 fu.vue 在父组件的方法中调用子组件的方法,很重要 this.$refs.mychild.parentHandlecl ...
 - 多线程状态与优先级、线程同步与Monitor类、死锁
			
一.线程状态 二.线程优先级 三.初步尝试多线程 class Program { static void Main(string[] args) { while (true) { MessagePri ...
 - mongodb的数据库,集合,数据可介绍。
			
我们知道,在关系型数据库里面有数据库.数据表.表里面是一行一行的数据.而mongodb是非关系型数据库,它有的是数据库.集合.文档,分别对应关系型里面的数据库.数据表.和表里面一行一行的数据.在mon ...
 - skkyk:题解 洛谷P2420 【让我们异或吧】lca+xor前缀和
			
刚学了LCA,写篇题解巩固一下 首先题目有误: (A是否是男生 )xor( B是否是男生)=A和B是否能够成为情侣,这句话显然是错误的qwq 对于这道题,容易看出,对于待处理的两个点,只要我们找到他的 ...
 - 《anchor-based v.s. anchor-free》
			
作者:青青子衿链接:https://www.zhihu.com/question/356551927/answer/926659692来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
 - zz高精地图和定位在自动驾驶的应用
			
本次分享聚焦于高精地图在自动驾驶中的应用,主要分为以下两部分: 1. 高精地图 High Definition Map 拓扑地图 Topological Map / Road Graph 3D栅格地图 ...
 - luoguP4094 [HEOI2016/TJOI2016]字符串
			
题意 考虑二分答案\(mid\),现在我们要判断\(s[c...c+mid-1]\)是否在\(s[a...b]\)出现过. 首先找到\(s[c...c+mid-1]\)所在的状态: 建出\(paren ...
 - 【day08】PHP
			
一. 函数 1.函数:封装的,可以重复使用的完成特定功能的代码段. 2.函数分类: (1)系统函数 (2)自定义函数 3.自定义函数 (1)格式 function 函数名称([参数[= ...
 - Linux宝塔安装步骤
			
首先:先运行 X shell 一:输入命令 Centos安装命令(一般都用这个): yum install -y wget && wget -O install.sh http://d ...