I have a custom view like following which I have been able to reduce to:
class MyCustomView : FrameLayout {
constructor(context: Context) : super(context) {
initView(null)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initView(attrs)
}
constructor(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int)
: super(context, attrs, defStyleAttr) {
initView(attrs)
}
private fun initView(attrs: AttributeSet?) {
attrs?.let {
context.theme.obtainStyledAttributes(attrs, R.styleable.MyCustomView, 0, 0).use {
if (it.hasValue(R.styleable.MyCustomView_my_custom_attribute)) {
//...
}
}
}
}
}
And the problem is I'm getting an Exception: android.content.res.TypedArray cannot be cast to java.lang.AutoCloseable when executing obtainStyledAttributes method.
The same method is used all over my project but this is the only place where it is crashing. Why is that?