2015-09-15 36 views
0

我想提出一個顯示快速撥號,但每當我運行應用程序,我得到的消息的Android - java.lang.InstantiationException:不能實例類:沒有空構造

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

} 
+0

你在哪裏叫班級Needle? –

+1

請發佈整個堆棧跟蹤。 – CommonsWare

+0

Strack的蹤跡,讓狗看到兔子 –

回答

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 

某處在你的清單,你有一個<activity>元素表明com.needle.views.NeedleActivity一個子類。

而且,在Java代碼的某個地方,您打電話給startActivity(),其中Intent指向com.needle.views.Needle

但是,com.needle.views.Needle不會從Activity繼承。它繼承自View。您不能將其用作活動。

你一定是歡迎建立一個不同的類,確實繼承Activity,使用Needle作爲其用戶界面的一部分。

相關問題