错误解决 Can't create handler inside thread that has not called Looper.prepare()
来源:互联网 发布:java管理系统源代码 编辑:程序博客网 时间:2024/05/09 02:54
作为菜鸡,一遇到这种BUG就一脸懵逼,最后还是疯狂的查询百度,得到了这样的解决方法,记录一下,自己测试是可行的。
首先一开始报错的代码是这样的:
@Override public void onClick(View v) { if (0 != musicProgressBar.getProgress()) { // TODO Auto-generated method stub startService();//自己写了个方法包装了开启服务+Toast //倒计时 new Timer().schedule(new TimerTask() { @Override public void run() { if(time > 0){ //在这里musicProgressBar是一个圆形的拖动条,setProgress是设置拖动条当前的值 musicProgressBar.setProgress(--time); if(0 == time){ //问题就出在这句Toast里 Toast.makeText(this, "时间到", Toast.LENGTH_SHORT).show(); cancel(); } } } }, 60*1000, 60*1000); }else{ Toast.makeText(this, "您还没有选择时间", Toast.LENGTH_SHORT).show(); } }
Can't create handler inside thread that has not called Looper.prepare()
我一开始也以为和Looper.prepare()这个方法有关,查了百度之后发现,Android好像不能再线程中更新UI,这句话我也不是很懂,然后需要把更新UI的这个过程放到主线程里,所以需要用到Handler这个东西,大概意思就是可以在其他线程(类似于Thread和Timer)中使用Handler 和Message。在需要更新UI(目前只知道Toast)的地方,使用Handler将一个Message发送出去,在主线程中接收这个Message,接收到之后就使用Toast或者其他更新UI的事情,大致原理就是这样。
以下是更改之后的代码
MainActivity中,设置Message的接受,以及Toast事件
private Handler mHandler;mHandler=new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case ID_USER: //获取传递的数据 Bundle data = msg.getData(); String text = data.getString("str"); //处理UI更新等操作 Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); stopService(); } }; };
@Override public void onClick(View v) { if (0 != musicProgressBar.getProgress()) { // TODO Auto-generated method stub startService(); //执行数据操作,不涉及到UI //倒计时 new Timer().schedule(new TimerTask() { @Override public void run() { /** * 用一个Handler来处理Toast */ Message msg = new Message(); //创建一个Message msg.what = ID_USER; //给这个Message一个标识 Bundle data = new Bundle(); //创建一个数据包 if(time > 0){ //在这里musicProgressBar是一个圆形拖动条,setProgress()是设定当前拖动位置 musicProgressBar.setProgress(--time); if(0 == time){//把这个数据包添加一个Key为str,后面这一段是我想要Toast的话 data.putString("str", "Times up! you can take a break!");//添加这段数据包到Message里 msg.setData(data);//使用Handler将Message发送出去 mHandler.sendMessage(msg); cancel(); } } } }, 60*1000, 60*1000); }else{ Toast.makeText(this, "您还没有选择时间", Toast.LENGTH_SHORT).show(); } }
最后这个错误就解决了,而且Toast也能显示出来。本人菜鸡一个,有错误可以指出来,谢谢
0 0
- Can't create handler inside thread that has not called Looper.prepare()错误的解决
- Can't create handler inside thread that has not called Looper.prepare()错误的解决
- 错误BUG解决: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() inside AsyncTask
- 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() 错误
- Can't create handler inside thread that has not called Looper.prepare() 错误有关问题
- Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()
- Android 报Can't create handler inside thread that has not called Looper.prepare()错误!
- 错误: Can't create handler inside thread that has not called Looper.prepare()
- Android开发报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()方法
- 解决Can't create handler inside thread that has not called Looper.prepare()
- Android问题集锦之五:Can't create handler inside thread that has not called Looper.prepare()错误的解决
- Handler——Can't create handler inside thread that has not called Looper.prepare()
- Gneymotion无法启动和Oracle VM VirtualBox常见问题
- Callable 和 FutureTask
- Hibernate 关系映射 many-to-many
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
- 算法库algorithm-0-概览
- 错误解决 Can't create handler inside thread that has not called Looper.prepare()
- Cesium viewer 最常用的控件 API
- extjs 循环执行多个异步请求时,参数和后台以及相关问题和衍生问题的处理
- Codeforces 91B-Queue
- Let’s Hook a Library Function_linux
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- HTTP版本和连接时长的解析
- 这是我的第一个博客
- 如何快速使用freemarker