roberto
03/20/2019, 2:36 AMclass MyAwesomeView: FrameLayout {
constructor(context: Context?, attrs: AttributeSet? = null) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, parm1: Int) : super(context, attrs, parm1)
constructor(context: Context?, attrs: AttributeSet?, parm1: Int, parm2: Int) : super(context, attrs, parm1, parm2)
init {
context.someMethod(attrs)
}
}
The init method cannot access attrs, I'm getting "Unresolved reference 'attrs'". Why is that? how can I make init access attrs. Shouldn't be enough by adding attrs to constructors as it is here?Nate
03/20/2019, 3:02 AMinit
is called after the constructor, not as the constructor body. Try making attrs
a valdawidhyzy
03/20/2019, 3:27 AMinit
is called after primary constructordawidhyzy
03/20/2019, 3:27 AMdawidhyzy
03/20/2019, 3:28 AMNate
03/20/2019, 3:34 AMdawidhyzy
03/20/2019, 3:36 AMroberto
03/20/2019, 3:38 AMstreetsofboston
03/20/2019, 3:42 AMprivate fun init(...)
and just call it from the body of all your secondary constructors.ghedeon
03/20/2019, 6:12 AMclass MyAwesomeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
: FrameLayout(context, attrs, defStyleAttr) {
init {
attrs...
}