Search code examples
androidkotlinandroid-custom-view

class extends AppCompatImageView Supertype initialization is impossible without primary constructor


There is a Custom Imageview class that extends AppCompatImageView

It shows error : Supertype initialization is impossible without primary constructor

class ImageViewVasl : AppCompatImageView() {
    constructor(context: Context) : super(context) {
        initialize(context = context, attrs = null)
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initialize(context = context, attrs = attrs)
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initialize(context = context, attrs = attrs)
    }
    private fun initialize(
        context: Context?,
        attrs: AttributeSet?
    ) {

    }
}

Solution

  • You need to call parent's constructor:

    class PieChart(context: Context, attrs: AttributeSet) : View(context, attrs)
    

    In your source code, I cannot see where you call the parent's constructor. More detail in this link : https://developer.android.com/training/custom-views/create-view