【问题标题】:Android - java.lang.InstantiationException: can't instantiate class : no empty constructorAndroid - java.lang.InstantiationException:无法实例化类:没有空的构造函数
【发布时间】:2015-09-15 20:59:44
【问题描述】:

我正在制作一个显示快速拨号的应用程序,但每当我运行该应用程序时,我都会收到消息

java.lang.InstantiationException:无法实例化类:没有空的构造函数

我该如何解决这个问题?

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

}

【问题讨论】:

  • 你在哪里调用 Needle 类?
  • 请发布整个堆栈跟踪。
  • Strack trace,让狗看到兔子
  • @MicheleLacorte Needle 是唯一的课程,它的主要活动
  • @CommonsWare “堆栈跟踪”是什么意思?对不起,我今天才开始使用 Stack Overflow

标签: java android exception runtime-error instantiation


【解决方案1】:
 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

在清单的某处,您有一个 &lt;activity&gt; 元素,表明 com.needle.views.NeedleActivity 的子类。

而且,在您的 Java 代码中,您正在调用 startActivity(),而 Intent 指向 com.needle.views.Needle

但是,com.needle.views.Needle 不继承自 Activity。它继承自View。您不能将其用作活动。

当然欢迎您创建一个确实继承自Activity 的不同类,该类使用Needle 作为其用户界面的一部分。

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多