小编Ale*_* L.的帖子

"处理程序类应该是静态的,否则可能会发生泄漏" - 处理程序引用了主要的活动变量

Android:我在我的活动中定义了一个Handler类,我得到警告"Handler类应该是静态的,否则可能会发生泄漏",代码如下:

    Handler messageHandler = new Handler() {
      // @Override 
      public void handleMessage(Message msg) {
        try {
            ... accessing variables defined at the activity level
            ... doing something very important
        }
        super.handleMessage(msg)
      }
    }
Run Code Online (Sandbox Code Playgroud)

但问题是我的消息处理程序有对主要活动变量的引用,所以我不能使它成为静态的.在我的情况下,如何摆脱这种警告(以正确的方式)?

variables static reference handler android-activity

2
推荐指数
1
解决办法
1385
查看次数

标签 统计

android-activity ×1

handler ×1

reference ×1

static ×1

variables ×1