Skip to content Skip to sidebar Skip to footer

Can't Instantiate Class ...; No Empty Constructor

When I run my PaintView.java, I receive the following error in logcat, namely can't instantiate class com.example.connectthedots.PaintView; no empty constructor. 07-22 18:47:43.453

Solution 1:

Caused by: java.lang.InstantiationException: can't instantiate class com.example.connectthedots.PaintView; no empty constructor

As the exception tells you, PaintView requires a constructor without any parameters :

publicPaintView() {
...
}

Solution 2:

add an empty Constructor:

publicPaintView() {

}

Solution 3:

I don't think simply adding an empty constructor is the best solution here. If you want to instantiate and use a PaintView object, you should to use one of the already defined constructors. If you take a look at the Android View Documentation, there is no empty constructor. Since you have a subclass of View, it doesn't really make sense to implement an empty constructor.

Instead, I would recommend you take a look at the code you are using to create the PaintView object. Right now, you probably have some thing like:

PaintViewmPaintView=newPaintView();

Instead, you should use one of the already implemented constructors:

PaintViewmPaintView=newPaintView(mContext);

Where mContext is a valid Context object in this instance. If you are doing this in an Activity, you could just pass the current instance of that Activity to the constructor.

Post a Comment for "Can't Instantiate Class ...; No Empty Constructor"