Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - no default constructor in view

Tags:

android

I have a problem with using a class that extends android.view.View, which is odd because I am doing the same thing in two different projects, and only in one of them is this an issue.

Both projects have a class which says: public class ClassName extends View.

But for one of them, this line is underlined red, and the message given is

"There is no default constructor available in 'android.view.View' "

I do not know why one has this problem while the other does not. They are not the same classes, naturally, but they both extend View in the same way. I thought that this would be a common problem but I'm not finding anything about it through Google searching, so I am asking here. Thank you for any help you can give!

EDIT:

The comments made me check again, and sure enough, the class with the error did not have a constructor defined in its class. I find it interesting that even though I went and defined a constructor, it still gave the same error until I added parameters (Context context, AttributeSet attrs) and in the constructor added a line 'super(context, attrs)'. I added these because they were present in the working class's constructor. So the working version is

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

My new question is about what exactly this does. This was taken from the class that didn't have the error, and that version was originally copied from a tutorial and never gave it much thought at the time.

like image 554
user2828965 Avatar asked Nov 25 '14 10:11

user2828965


1 Answers

Delete construct without params.

Your view should have only 3 constructors:

public DrawingActivity(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public DrawingActivity(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}
public DrawingActivity(Context context) {
    super(context);
} 

About this constructors you can read here. I can mention only about defStyle:

The default style to apply to this view. If 0, no style will be applied (beyond what is included in the theme). This may either be an attribute resource, whose value will be retrieved from the current theme, or an explicit style resource.

like image 169
Suvitruf - Andrei Apanasik Avatar answered Nov 12 '22 01:11

Suvitruf - Andrei Apanasik