android - Kotlin by Lazy in Custom Views -
currently, using by lazy
in custom view break visualisation in android studio. using iseditmode
can't used because available inside method , not @ variable initialisation scope. workaround using lateinit , or delegates.notnull().
is there workaround this?
example:
class imageviewcirclebg : imageview { private var circlebgcolor = r.color.colorprimary private val paint lazy { paint().apply { style = paint.style.fill isantialias = true } } constructor(context: context) : this(context, null) constructor(context: context, attrs: attributeset?) : this(context, attrs, 0) constructor(context: context, attrs: attributeset?, defstyleattr: int) : super(context, attrs, defstyleattr) { val ta = context.theme.obtainstyledattributes(attrs, r.styleable.imageviewcirclebg, 0, 0) circlebgcolor = ta.getcolor(r.styleable.imageviewcirclebg_roundbgcolor, circlebgcolor) ta.recycle() this.paint.color = circlebgcolor }
produces following error in android studio design window:
the following classes not instantiated: - ....imageviewcirclebg (open class, show exception, clear cache) tip: use view.isineditmode() in custom views skip code or show sample data when shown in ide exception details java.lang.incompatibleclasschangeerror: found class kotlin.lazy, interface expected at ...imageviewcirclebg.getpaint(imageviewcirclebg.kt:-1)
replacing lazy this:
private val paint: paint init { paint = paint().apply { style = paint.style.fill isantialias = true } }
solves problem.
error:
java.lang.incompatibleclasschangeerror: found class kotlin.lazy, interface expected
Comments
Post a Comment