This handler class should be static or leaks might occur

rosecshly0928 2012-07-13 06:12:39
我的service有个内部类Servicehandler继承自Handler,new 这个ServiceHandler的那行提示“This handler class should be static or leaks might occur”。
我这个ServiceHandler不能为static,里面有非静态的成员变量,应该怎么办?

...全文
3185 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hvk687 2012-07-16
  • 打赏
  • 举报
回复
去看messsage的说明对message.obj的说明
rosecshly0928 2012-07-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

Context能这样传吗,传的必须是Parcelable,Context又不是Parcelable.
[/Quote]
不知道能不能这样传,那个地方说“传的必须是Parcelable”?
hvk687 2012-07-15
  • 打赏
  • 举报
回复
Context能这样传吗,传的必须是Parcelable,Context又不是Parcelable.
rosecshly0928 2012-07-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

这个有什么关系?
[/Quote]
如果ServiceHandler申明为static的,里面的:
Context context=(Context) msg.obj;
int hour=msg.arg1;
int minute=msg.arg2;
不是static的啊
hvk687 2012-07-15
  • 打赏
  • 举报
回复
这个有什么关系?
rosecshly0928 2012-07-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你的内部类中没有访问service中的成员没有?如果没有就没关系,内部类声明为static,可以和普通类一样使用,而不需要绑定到外部类的对象上
[/Quote]
没有访问外部类的成员,但是有传进来参数啊,这就是那个类
final class ServiceHandler extends Handler{

public ServiceHandler(Looper looper) {
super(looper);
// TODO Auto-generated constructor stub
}

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
if(msg.what==1)
{
Context context=(Context) msg.obj;
int hour=msg.arg1;
int minute=msg.arg2;
boolean start=true;
}
if(msg.what==2)
{
}
}
}
hvk687 2012-07-13
  • 打赏
  • 举报
回复
你的内部类中没有访问service中的成员没有?如果没有就没关系,内部类声明为static,可以和普通类一样使用,而不需要绑定到外部类的对象上

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧