1

在我的手机上运行这个 android appp 时,当我单击启动应用程序并且 logcat 显示以下错误时,它突然崩溃。

> 07-24 14:54:03.291
> 15207-15207/com.example.parthibank.needleandroidproj E/AndroidRuntime:
> FATAL EXCEPTION: main
>                                                                                           Process: com.example.parthibank.needleandroidproj, PID: 15207
>                                                                                           java.lang.RuntimeException: Unable to instantiate activity
> ComponentInfo{com.example.parthibank.needleandroidproj/com.example.parthibank.needleandroidproj.MainActivity}:
> java.lang.InstantiationException:
> java.lang.Class<com.example.parthibank.needleandroidproj.MainActivity>
> has no zero argument constructor
>                                                                                               at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133)
>                                                                                               at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
>                                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:229)
>                                                                                               at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
>                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
>                                                                                               at android.os.Looper.loop(Looper.java:148)
>                                                                                               at android.app.ActivityThread.main(ActivityThread.java:7407)
>                                                                                               at java.lang.reflect.Method.invoke(Native Method)
>                                                                                               at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
>                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
>                                                                                            Caused by: java.lang.InstantiationException:
> java.lang.Class<com.example.parthibank.needleandroidproj.MainActivity>
> has no zero argument constructor
>                                                                                               at java.lang.Class.newInstance(Native Method)
>                                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
>                                                                                               at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
>                                                                                               at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
>                                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
>                                                                                               at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
>                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
>                                                                                               at android.os.Looper.loop(Looper.java:148) 
>                                                                                               at android.app.ActivityThread.main(ActivityThread.java:7407) 
>                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
>                                                                                               at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
>                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我无法理解它要求哪个承包商。请指导

 package com.example.parthibank.needleandroidproj;


            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 MainActivity 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 MainActivity(Context context) {
            super(context);
            matrix = new Matrix();
            this.startTime = System.currentTimeMillis();
            this.postInvalidate();
            init();
        }

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

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

    }
4

1 回答 1

0

您需要定义一个no-args constructor,如下所示class

public MainActivity() {
super();
}
于 2017-07-24T11:44:26.727 回答