Skip to content Skip to sidebar Skip to footer

Custom View Is Missing Constructor Used By Tools For Adapter

I got the following warning: Custom view com/example/view/adapter/SomeAdapter is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSe

Solution 1:

In your CustomView class add constructors:

public CustomView(Context context) {
    super(context);
}
public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

Solution 2:

Please try below in Kotlin:-

classCustomView: View {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) {
    }
}

Solution 3:

Some layout tools (such as the Android layout editor for Eclipse) needs to find a constructor with one of the following signatures: * View(Context context) * View(Context context, AttributeSet attrs) * View(Context context, AttributeSet attrs, int defStyle)

If you are not using such things, and just want to get rid of this warning for all your projects just go to:

Window -> Preferences -> Android -> Lint Error Checking.

Find ViewConstructor from the list and set the severity to 'ignore'.

Solution 4:

You are using a custom View class, you'll need to add the missing constructors to your class.

In Java, you can use Jarvis' answer:

classYourCustomView {
    publicYourCustomView(Context context) {
        super(context);
    }
    publicYourCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

It's more more compact in Kotlin:

classYourCustomView@JvmOverloadsconstructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)

Post a Comment for "Custom View Is Missing Constructor Used By Tools For Adapter"