Android - no default constructor

I have a problem 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 the problem.

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

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

"There is no default constructor in the android.view.View constructor

I do not know why you have this problem, and the other is not. Naturally, they are not the same classes, but both of them simultaneously expand the view. I thought this would be a common problem, but I can't find anything about it through a google search, so I ask here. Thanks for any help you can give!

EDIT:

The comments made me check again, and, of course, the class with an error does not have a constructor defined in its class. I'm curious that although I went and determined the constructor, it still threw the same error until it added the parameters (context context, AttributeSet attrs), and the string "super (context, attrs)" was added to the constructor. I added them because they were present in the constructor of the working class. So the working version

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

My new question is what exactly does this do. This was taken from a class that did not have an error, and this version was originally copied from a textbook and never thought about it.

+6
source share
3 answers

Delete a structure without parameters.

There should be only 3 constructors in your view:

 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); } 

You can read about these designers here . I can only mention defStyle:

The default style applied to this view. If 0, no style will be (other than what is included in the theme). This can be an attribute resource whose value will be extracted from the current topic or an explicit style resource.

+6
source

I do not think this is his problem ... This does not work:

 `public MyPanel(Context _context) { context = _context; super(context);` 

It works:

 `public MyPanel(Context _context) { super(_context); context = _context;` 

I guess it was his problem, or at least it was my problem. You get the same error that it describes.

0
source

Your class must have two constructors:

 public class ABC extends View{ public ABC(Context context) { super(context); } public ABC(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }} 
0
source

Source: https://habr.com/ru/post/978674/


All Articles