解决bug:运行项目时报异常 “Can't create handler inside thread that has not called Looper.prepare()”——诺诺"涂鸦"记忆
来源:互联网 发布:包包设计软件 编辑:程序博客网 时间:2024/05/19 00:56
---------- 诺诺学习技术交流博客、期待与您交流! ----------
详情请查看:http://blog.csdn.net/sun_promise
bug: “Can't create handler inside thread that has not called Looper.prepare()”
1.问题描述
在Android Studio中运行项目时报异常: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
2.分析原因
Can't create handler inside thread that has not called Looper.prepare()
翻译过来的意思是:在子线程thread中不能创建handler,没有叫Looper.prepare()的方法。
分析:实际项目中是有创建了handler的,那为什么在子线程中还要创建handler呢?那最大的可能就是在子线程中有某些地方做了UI操作,并没有放到handler中去执行。所以运行时才会报错。记住一个原则,主线程不能做耗时操作,子线程中不能做更改UI操作,子线程需要通过handler才能来做更改UI的相关操作。
Note:若报这样的错误就去你的子线程中查找问题代码。一般开发工具都会提示哪里有错误的。细心点就ok了。
3.案例分析:
1)项目中报错如下:
04-21 10:16:52.930 17728-18371/com.******.debug E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1385 Process: com.******.debug, PID: 17728 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.<init>(Handler.java:200) at android.os.Handler.<init>(Handler.java:114) at android.widget.Toast$TN.<init>(Toast.java:338) at android.widget.Toast.<init>(Toast.java:100) at android.widget.Toast.makeText(Toast.java:249) at com.******.app.base.PromptManager.showShortToast(PromptManager.java:173) at com.******.app.utils.APKVersionUpdate$3.run(APKVersionUpdate.java:193)
2)涉及报错的代码:
(注:只是为了说明问题,并没有贴出项目全部源码,所以只贴出了报错位置部分的代码)
new Thread() {@Overridepublic void run() {try {//此处代码什么功能不重要,final File file = getFileFromServer(url, updateProgress);sleep(2000);installApk(file); if(updateProgress.isShowing()){ updateProgress.colseDialog(); }} catch (Exception e) {//我在此处做了更改UI的操作,才会报错//这是自定义的吐司 PromptManager.showShortToast(TextActivity.this, "升级失败!请检查网络后重试"); //这是取消进度条对话框操作 if(updateProgress.isShowing()){ updateProgress.colseDialog(); }}}}.start();
3)更正后的代码:
在子线程中:
new Thread() {@Overridepublic void run() {try {//此处代码什么功能不重要,final File file = getFileFromServer(url, updateProgress);sleep(2000);installApk(file); if(updateProgress.isShowing()){ updateProgress.colseDialog(); }} catch (Exception e) {handler.sendEmptyMessage(0);}}}.start();在Handler中:
private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) { switch (msg.what){ case 0://更新UI操作 PromptManager.showShortToast(MinxinApplication .getInstance().getApplicationContext(), "升级失败!请检查网络后重试"); if(updateProgress.isShowing()){ updateProgress.colseDialog(); } break; default:break; }};};
---------- 诺诺学习技术交流博客、期待与您交流! ----------
详情请查看:http://blog.csdn.net/sun_promise
0 0
- 解决bug:运行项目时报异常 “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()方法
- Handler——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
- Handler机制和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()异常
- 异常处理 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()
- studio 异常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()错误的解决
- 解决Can't create handler inside thread that has not called Looper.prepare()
- LINQ Contact/Union/Intersect/Skip/Except/pageing/Link操作
- VS2010+QT5编程之setWindowIcon报错解决方案
- qt之undefined reference to vtable for出现原因
- stm32f407建立工程模板
- 字符串排序
- 解决bug:运行项目时报异常 “Can't create handler inside thread that has not called Looper.prepare()”——诺诺"涂鸦"记忆
- UIActionSheet的最后一个按钮 不响应的问题
- 黑马程序员_Map集合中按value值进行排序
- 虚拟机上网问题
- SpringMVC注解控制器详解
- jenkins中记录Unity AssetServer的日志
- 内存使用准则
- HLS 基本知识及环境搭建
- android adb shell 命令大全