Handler 相关的问题
2015-09-07 17:44
295 查看
1. Android Studio 不能自动 import class android.os.Handler,而是直接 import class java.util.logging.Handler,这是什么鬼?难道是因为 Handler 实例不能直接 new?
似乎是 Android Studio 的一个缺陷,手动 import 吧
2. Android Studio 黄色警告,可能会有泄漏,这个怎么处理?
final android.os.Handler myHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 0x123) {// do sth}super.handleMessage(msg);}};
Handler reference leaks
Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected.
If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue.
If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows:
Declare the Handler as a static class;
In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler;
Make all references to members of the outer class using the WeakReference object.
再看吧。。。
3. 因为只由主线程可以对UI进行修改,所以是不是只由主线程有自己的 Looper,“程序员自己启动的子线程,程序员必须自己创建一个Looper对象,并启动”的意义何在?子线程并不需要Looper来进行UI修改啊,不是也改不了么?是自定义的Activity要这么来做?
的确可以在不需要使用到Looper的时候进行Handler的使用,另外在子线程中使用handler 然后更新UI,还是会导致异常,但是使用 Toast.show() 没有问题。[code]CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
相关文章推荐
- IOS Certificates, Identifiers, Devices, Profiles
- ORACLE expdp/impdp导出实例
- 数据库资料下载大全
- Swift 编程语言入门教程
- HTML5本地存储——IndexedDB(一:基本使用)
- Android Studio中jar与aar 的生成与aar导入项目方法
- Windows AndroidStudio快捷键与Eclipse对比
- java abstract类和interface的区别,精辟!
- java enum浅析
- 老大让我写个表单验证的插件,市面上的表单验证插件不再少数。但是老大让我写肯定是想锻炼我
- 推荐大家使用的CSS书写规范、顺序
- java基本类型和字符串的转换
- C# 使用 ExcelLibrary 读写 Excel 文件
- Gradle eclipse识别JavaWeb项目相关蛋疼问题
- 美团和百度都遇到的问题
- Manacher算法--O(n)回文子串算法
- hdu 5241__Friends
- 静态区-栈区-堆区区别
- Java获取客户端真实IP地址的两种方法
- MAC OS 9个实用的终端命令