嗨,我正在使用以下代码,因此在发生崩溃循环时将始终调用该调用以释放内存。
context.obtainStyledAttributes(attrs, R.styleable.example).apply {
try {
getDrawable(R.styleable.example_drawable)
} finally {
recycle()
}
}
但是,由于某些原因,android studio lint无法检测到回收调用,并且它总是抱怨,给我这个警告:
This 'TypedArray' should be recycled after use with '#recycle()'
我也尝试过使用Kotlin扩展功能,该功能应自动为我调用回收站:
context.obtainStyledAttributes(attrs, R.styleable.example).use
{
it.getDrawable(R.styleable.example_drawable)
}
但是棉绒警告仍然存在。知道该怎么办吗?
我当前正在使用最新版本的Android Studio,Gradle和Kotlin(分别为3.5.2、6.0.1和1.3.60)
也许有办法更新皮棉吗?还是它已经随Android Studio一起提供?谢谢。
请参见How to use resource arrays using xml in Android?。
尝试中封装整个结构-捕获:
var drawables = ...
try {
drawables = context.obtainStyledAttributes(attrs, R.styleable.example)
drawables.getDrawable(R.styleable.example_drawable)
} finally {
drawables.recycle()
}
抱歉,我没有测试。