This Handler class should be static or leaks might occur Android
来源:互联网 发布:逆战显示网络连接断开 编辑:程序博客网 时间:2024/06/01 10:45
首先解释下这句话This Handler class should be static or leaks might occur,大致意思就是说:Handler类应该定义成静态类,否则可能导致内存泄露。
具体如何解决,在国外有人提出,如下:
Issue: Ensures that Handler classes do not hold on to a reference to an outer class
In Android, Handler classes should be static or leaks might occur. Messages enqueued on the application thread's MessageQueue also retain their target Handler. If the Handler is an inner class, its outer class will be retained as well. To avoid leaking the outer class, declare the Handler as a static nested class with a WeakReference to its outer class.
大体翻译如下:
Handler 类应该应该为static类型,否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类,那 么它也会保持它所在的外部类的引用。为了避免泄露这个外部类,应该将Handler声明为static嵌套类,并且使用对外部类的弱应用。
使用范例:
static
class
MyHandler
extends
Handler {
WeakReference<PopupActivity> mActivity;
MyHandler(PopupActivity activity) {
mActivity =
new
WeakReference<PopupActivity>(activity);
}
@Override
public
void
handleMessage(Message msg) {
PopupActivity theActivity = mActivity.get();
switch
(msg.what) {
case
0
:
theActivity.popPlay.setChecked(
true
);
break
;
}
}
};
MyHandler ttsHandler =
new
MyHandler(
this
);
private
Cursor mCursor;
private
void
test() {
ttsHandler.sendEmptyMessage(
0
);
}
- This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur
- This handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur.
- Android 关于Handler警告处理 This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur Android
- This Handler class should be static or leaks might occur Android
- This Handler class should be static or leaks might occur Android
- This Handler class should be static or leaks might occur Android
- Android“This Handler class should be static or leaks might occur”警告的处理方法
- This Handler class should be static or leaks might occur Android
- android之lint警告This Handler class should be static or leaks might occur
- This Handler class should be static or leaks might occur Android
- 安装intelhaxm出错,this computer does not support intel virtualization Technology(VT-x)
- 改善Java文档的理由、建议和技巧
- 破解&屏蔽防止嵌入框架代码 top.location != self.location
- Android 友盟更新
- Windows API技术的演化
- This Handler class should be static or leaks might occur Android
- 如何在eclipse中快速创建Activity
- thinkpad 左键变成右键 or 自动点右键
- 简析truncate和delete的区别
- 函数式编程
- 高阶函数
- 大整数的加乘多项式运算
- 生鲜电商的七种模式分析
- 【leetcode每日一题】61.Rotate List