Custom view is missing constructor used by tools for adapter
Asked Answered
C

4

16

I got the following warning:

Custom view com/example/view/adapter/SomeAdapter is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int)

in my class SomeAdapter which extends some BaseAdapter which extends ArrayAdapter

public class SomeAdapter extends BaseAdapter{}
public abstract class BaseAdapter extends ArrayAdapter<SomeModel>{}

The warning exists in the concrete adapter but not in the abstract BaseAdapter. Has anyone ever heard of this warning in that context?

AFAIK Android checks classes for they are extending views by checking the name of the super classes via ViewConstructorDetector:

 private static boolean isViewClass(ClassContext context, ClassNode node) {
    String superName = node.superName;
    while (superName != null) {
        if (superName.equals("android/view/View")                //$NON-NLS-1$
                || superName.equals("android/view/ViewGroup")    //$NON-NLS-1$
                || superName.startsWith("android/widget/")       //$NON-NLS-1$
                && !((superName.endsWith("Adapter")              //$NON-NLS-1$
                        || superName.endsWith("Controller")      //$NON-NLS-1$
                        || superName.endsWith("Service")         //$NON-NLS-1$
                        || superName.endsWith("Provider")        //$NON-NLS-1$
                        || superName.endsWith("Filter")))) {     //$NON-NLS-1$
            return true;
        }

        superName = context.getDriver().getSuperClass(superName);
    }

    return false;
}

As far as I can see my class names don't match the pattern above. Does anyone has any idea how to fix or suppress this warning?

getView() in BaseAdapter:

@Override
public final View getView(final int position, final View convertView, final ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        view = createNewView(parent, position);
    } else {
        reuseOldView(view, position);
    }
    return view;
}
Concertante answered 12/6, 2013 at 10:22 Comment(4)
What implements your adapter? Do you have a custom view?Extraterritoriality
No, I don't implement anything. Just extending the ArrayAdapter.Concertante
for what purpose? A adapter cannot work on its own. What do you want to use it for?Extraterritoriality
I use may adapter in a (standard) ListView.Concertante
A
12

In your CustomView class add constructors:

public CustomView(Context context) {
    super(context);
}
public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
Alenaalene answered 12/6, 2013 at 10:29 Comment(3)
I don't have any CusonView. I just have my Adapters. And the ArrayAdapter does not provide such constructors.Concertante
Could you post your getView() method from your adapters? ThanksAlenaalene
I'm not sure what is this good for, but here you go. The getView() is implemented in the BaseAdapter but not in the SomeAdapter, where the warning is.Concertante
H
4

Please try below in Kotlin:-

 class CustomView: 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) {
    }
}
Haydenhaydn answered 22/7, 2019 at 3:53 Comment(0)
B
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'.

Boresome answered 11/7, 2013 at 16:49 Comment(1)
Does this influence the layout editor? What I don't understand is why does Lint think my adapter is a view.Concertante
B
1

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:

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

It's more more compact in Kotlin:

class YourCustomView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr)
Britska answered 17/11, 2019 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.