Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,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;
}
like image 822
salcosand Avatar asked Jun 12 '13 10:06

salcosand


2 Answers

In your CustomView class add constructors:

public CustomView(Context context) {
    super(context);
}
public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
like image 190
Jarvis Avatar answered Nov 18 '22 03:11

Jarvis


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) {
    }
}
like image 4
Shalu T D Avatar answered Nov 18 '22 02:11

Shalu T D