该字段泄漏上下文对象 [英] This field leaks a context object

查看:254
本文介绍了该字段泄漏上下文对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

class HttpRequestTask(private val debtWsUrl : URI, debtorText : TextView) : 
      AsyncTask<Void, Void, Iterable<Debtor>?>() {
            val debtorText: TextView = debtorText
}

为什么TextView行显示警告:

Why line with TextView shows warning:

此字段泄漏上下文对象

This field leaks a context object

?

如何防止这种情况?

推荐答案

您将视图分配给HttpRequestTask.由于视图需要上下文,因此您正在泄漏它.

You assign a View to your HttpRequestTask. Since a View requires a Context you are leaking it.

只需考虑一下,如果View已被破坏但Http Task尚未完成,将会发生什么情况.

Just think what happen if the View has been destroyed but the Http Task is not finished yet.

这就是为什么您应该避免在方法中分配与Context相关的东西,而在视图被杀死之前,这些东西可能会有所回报.

Thats why you should avoid assigning Context relevanted stuff inside methods which may give something back while the view has been already killed.

删除债务人文本并返回值以将其设置在视图中.

Remove the debtorText and return the value to set it inside your view.

这篇关于该字段泄漏上下文对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆