我有以下代码:
public RVIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray attributes = context.obtainStyledAttributes(
attrs, R.styleable.RVIndicator, defStyleAttr, R.style.RVIndicator);
dotColor = attributes.getColor(R.styleable.RVIndicator_dotColor, 0);
selectedDotColor = attributes.getColor(R.styleable.RVIndicator_dotSelectedColor, dotColor);
dotNormalSize = attributes.getDimensionPixelSize(R.styleable.RVIndicator_dotSize, 0);
spaceBetweenDotCenters = attributes.getDimensionPixelSize(R.styleable.RVIndicator_dotSpacing, 0) + dotNormalSize;
looped = attributes.getBoolean(R.styleable.RVIndicator_looped, false);
int visibleDotCount = attributes.getInt(R.styleable.RVIndicator_visibleDotCount, 0);
setVisibleDotCount(visibleDotCount);
visibleDotThreshold = 0;
attributes.recycle();
paint = new Paint();
paint.setAntiAlias(true);
if (isInEditMode()) {
setDotCount(visibleDotCount);
onPageScrolled(visibleDotCount / 2, 0);
}
}
对于以下语句,lint 给出了警告:“使用 'TypedArray' 时没有使用 'try-with-resources' 语句”。如何解决此警告?
TypedArray attributes = context.obtainStyledAttributes(
attrs, R.styleable.RVIndicator, defStyleAttr, R.style.RVIndicator);
TypedArray.close()
实际上调用了recycle()
。另外,从 API 31 (S) 开始才支持 try-with-resources。 - MatthieuTypedArray.close()
实际上调用了recycle()
方法。另外,需要注意的是,使用 try-with-resources 语法只能在 API 31 (S) 及以上版本中使用。 - Matthieu