0

I am making an app that displays a speed dial but whenever I run the app, I get the message

java.lang.InstantiationException: can't instantiate class : no empty constructor

How do I fix this?

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class Needle extends View {

private Paint linePaint;
private Path linePath;
private Paint needleScrewPaint;

private Matrix matrix;
private int framePerSeconds = 100;
private long animationDuration = 10000;
private long startTime;

public Needle(Context context) {
    super(context);
    matrix = new Matrix();
    this.startTime = System.currentTimeMillis();
    this.postInvalidate();
    init();
}

public Needle(Context context, AttributeSet attrs) {
    super(context, attrs);
    matrix = new Matrix();
    this.startTime = System.currentTimeMillis();
    this.postInvalidate();
    init();
}

public Needle(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    matrix = new Matrix();
    this.startTime = System.currentTimeMillis();
    this.postInvalidate();
    init();
}

private void init(){

    linePaint = new Paint();
    linePaint.setColor(Color.RED); // Set the color
    linePaint.setStyle(Paint.Style.FILL_AND_STROKE); // set the border and fills the inside of needle
    linePaint.setAntiAlias(true);
    linePaint.setStrokeWidth(5.0f); // width of the border
    linePaint.setShadowLayer(8.0f, 0.1f, 0.1f, Color.GRAY); // Shadow of the needle

    linePath = new Path();
    linePath.moveTo(50.0f, 50.0f);
    linePath.lineTo(130.0f, 40.0f);
    linePath.lineTo(600.0f, 50.0f);
    linePath.lineTo(130.0f, 60.0f);
    linePath.lineTo(50.0f, 50.0f);
    linePath.addCircle(130.0f, 50.0f, 20.0f, Path.Direction.CW);
    linePath.close();

    needleScrewPaint = new Paint();
    needleScrewPaint.setColor(Color.BLACK);
    needleScrewPaint.setAntiAlias(true);
    needleScrewPaint.setShader(new RadialGradient(130.0f, 50.0f, 10.0f,
            Color.DKGRAY, Color.BLACK, Shader.TileMode.CLAMP));
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);


    long elapsedTime = System.currentTimeMillis() - startTime;

    matrix.postRotate(1.0f, 130.0f, 50.0f); // rotate 10 degree every second
    canvas.concat(matrix);

    canvas.drawPath(linePath, linePaint);

    canvas.drawCircle(130.0f, 50.0f, 16.0f, needleScrewPaint);

    if(elapsedTime < animationDuration){
        this.postInvalidateDelayed(10000 / framePerSeconds);
    }

    //this.postInvalidateOnAnimation();
    invalidate();
}

}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Aadhrik
  • 1
  • 2
  • Where are you calling the class Needle ? – Michele Lacorte Sep 15 '15 at 21:00
  • 1
    Please post the entire stack trace. – CommonsWare Sep 15 '15 at 21:01
  • Strack trace, let the dog see the rabbit – Mark Gilchrist Sep 15 '15 at 21:02
  • @MicheleLacorte Needle is the only class, its the main activity – Aadhrik Sep 15 '15 at 22:10
  • @CommonsWare What do you mean "stack trace"? Im sorry i just started using Stack Overflow today – Aadhrik Sep 15 '15 at 22:11
  • "What do you mean "stack trace"?" -- I mean the Java stack trace, showing where your exception came from. See [this Stack Overflow answer](http://stackoverflow.com/a/3988794/115145) to learn what a Java stack trace is. See [this Stack Oveflow answer](https://stackoverflow.com/a/23353174/115145) to learn how to get it from Android Studio or Eclipse. – CommonsWare Sep 15 '15 at 22:16
  • "Needle is the only class, its the main activity" -- no, it is not. `public class Needle extends View`. It is not a subclass of `Activity`. It is a subclass of `View`. – CommonsWare Sep 15 '15 at 22:16
  • It's fairly straightforward- you need an empty constructor. `Needle(){....}` – Daniel M. Sep 16 '15 at 01:18
  • @DanielM. I've tried this but it hasnt worked – Aadhrik Sep 16 '15 at 19:06
  • @DanielM. I get this message "There is no default constructor available in android.view.View" – Aadhrik Sep 16 '15 at 19:07
  • @CommonsWare java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{needle.ibizsmart.com.needle/com.needle.views.Needle}: java.lang.InstantiationException: can't instantiate class com.needle.views.Needle; no empty constructor – Aadhrik Sep 16 '15 at 19:10

1 Answers1

0
 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{needle.ibizsmart.com.needle/com.needle.views.Needle}: java.lang.InstantiationException: can't instantiate class com.needle.views.Needle; no empty constructor

Somewhere in your manifest, you have an <activity> element that indicates that com.needle.views.Needle is a subclass of Activity.

And, somewhere in your Java code, you are calling startActivity(), with an Intent that points to com.needle.views.Needle.

However, com.needle.views.Needle does not inherit from Activity. It inherits from View. You cannot use it as an activity.

You are certainly welcome to create a different class that does inherit from Activity, that uses Needle as part of its user interface.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491