Can't create handler inside thread that has not called Looper.prepare() 解决办法
在开发的过程当中,遇到了想在一个线程中弹出一个提示窗 new AlertDialog.Builder(
context),但是就出现了一个问题。
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
分析了一下原因,应该是不能在线程中操作UI界面。
然后我就就在线程里面新开一个线程,再配上hangler通信 但是还是有错误,最后查看了handler才知道是没有getMainLooper()。
因为在UI主线程之外是无法对UI组件进行控制的。因为你必须在新线程任务完成之后利用各种方法先UI主线程发送消息通知任务完成从而来显示各种提示消息。
最后变成这样,搞掂了
new Thread() {@Overridepublic void run() {Message msg = new Message();msg.what = 0;handler.sendMessage(msg);}}.start();handler = new Handler(context.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {if (0 == msg.what) {new AlertDialog.Builder(context).setTitle("提示").setIcon(R.drawable.icon).setMessage("确定要回到打乱状态吗!!").setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface arg0,int arg1) {rBlockGroup.setmap(replayStart);gamestart = GAMESTATR_REPLAY;// replayCount = 0;// repalyCurrentStep =// 0;replayState = GAMESTATR_RUN;}}).setNegativeButton("取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0,int arg1) {}}).create().show();}}};
转载于:https://www.cnblogs.com/mczha/p/3655875.html
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.prepare()
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() ...
- Can't create handler inside thread that has not called Looper.prepare()
问题: Can't create handler inside thread that has not called Looper.prepare() 1,在报错的方法前加Looper.prepare ...
- Android --- java.lang.RuntimeException: Can‘t create handler inside thread that has not called Loop
报错信息如下: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr ...
- java.lang.RuntimeException: Can't create handler inside thread that has not
在子线程中 调用了这句: Toast.makeText(UiUtils.getContext(), "正在下载"+name, Toast.LENGTH_SHORT).show(); ...
- Can't create handler inside thread Thread that has not called Looper.prepare()
今天在子线程里面写了一个Toast 运行程序直接崩溃了 看了下log 如下 最后忽然想到了子线程里面不能直接使用Toast 然后 就添加了 Looper.prepare(); 和 Looper.loo ...
- Android在子线程里使用Toast报错Can't toast on a thread that has not called Looper.prepare()
在接android SDK的时候有时候为了方便debug调试查看,通过Toast输出相关信息, 实际上这个是在子线程中输出的,在logcat里查看有如下报错java.lang.RuntimeExcep ...
- 使用git上传工程出现fatal:Unable to create ‘/xxx/xxx/xxx/.git/index.lock‘:File exists的解决办法
git上传工程出现fatal:Unable to create '/xxx/xxx/xxx/.git/index.lock':File exists的原因及解决方法 先说解决办法 再来说说原因 官方说 ...
- 安装oh-my-zsh报错could not create work tree dir '/root/.oh-my-zsh'.: Permission denied的原因解决办法
前言 linux新手可能会通过su方式切换用户,但没有意识到环境变量没有切换,导致一些奇葩问题,这里便是一个例子,值得记录. 笔者在切换linux用户使用ZSH时遇到问题,确切来说是切换用户后安装oh ...
- Exception in thread main java.lang.UnsupportedClassVersionError的另类解决办法
最近在Linux虚拟机上跑在windows平台上的eclipes打出来的jar包时报出Exception in thread "main" java.lang.Unsupporte ...
最新文章
- c语言定义字符类型变量的关键字,C语言数据类型
- linux /etc/profile和/etc/bashrc
- vb6 获得计算机硬件信息_计算机硬件系统由哪几部分组成
- Python是一门什么样的语言
- jmeter安装包_分布式执行jmeter脚本步骤(非GUI)
- iPhone 12系列起售价又要涨了?128GB起步...
- 太实用了,4款常用的可视化大屏工具,你一定要知道!
- springboot国际化04
- 小程序优化的20中策略
- linux内存的active,Linux性能优化和监控系列(三)——分析Memory使用状况
- 洛谷oj---P1030 求先序排列
- coursera和udacity_从Udacity和Coursera进行深度学习
- sql盲注特点_sql盲注
- java 基础:方法/函数
- 网络安全工程师的简单介绍
- html5怎么写副标题,毕业论文副标题怎么写
- 【Multisim仿真】运放电路:反相比例运算电路
- 宝塔服务器源代码修改记录,宝塔BT面板修改相关记录,所有插件免费用
- IDEA 将项目打包war包
- 手写JavaScript
热门文章
- 通过显示当前 python 程序占用的内存大小来比较生成器和迭代器(转载)
- Intellij idea导入项目时没有目录结构
- python 配置文件对比_difflib模块对比apache配置文件差异
- lasso回归_线性回归amp;lasso回归amp;岭回归介绍与对比
- java培训第一阶段测试总结,达内学员Java培训阶段总结:反躬自省,愈渐完美
- node遍历给定目录下特定文件,内容合并到一个文件
- linux安装python3.6以后报错处理
- iOS 无法获取 WiFi 列表?一定是因为你不知道这个框架
- Ueditor/自定义配置
- SQL Server 2005 在只有MDF文件的情况下恢复(丢失日志文件)