1

While running this android appp on my phone it crashes abruptly when i click to Launch the app and the logcat Shows follwing error.

> 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)

I am unable to understand which contructor is it asking for. Please guide

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

    }
GoGam
  • 61
  • 4

1 Answers1

0

You need to define a no-args constructor, like this in your class :

public MainActivity() {
super();
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I added "public class MainActivity extends View { public MainActivity() {}" Shows following error:-- Error:(16, 27) error: no suitable constructor found for View(no arguments) constructor View.View(Context) is not applicable (actual and formal argument lists differ in length) constructor View.View(Context,AttributeSet) is not applicable (actual and formal argument lists differ in length) constructor View.View(Context,AttributeSet,int) is not applicable constructor View.View(Context,AttributeSet,int,int) is not applicable (actual and formal argument lists differ in length) – GoGam Jul 24 '17 at 12:03
  • add logcat with quetion – AskNilesh Jul 24 '17 at 12:05
  • But there is a Compiler error after i added the empty contructor which u told. – GoGam Jul 24 '17 at 12:07
  • i told you beacuse your error is Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor – AskNilesh Jul 24 '17 at 12:09
  • try one thing declare your variable null in that empty constructor matrix = nnull; this.startTime = System.currentTimeMillis(); this.postInvalidate(); init() – AskNilesh Jul 24 '17 at 12:12
  • i ahve added the logcat error in main post please check. i will try nulling as u told now – GoGam Jul 24 '17 at 12:58
  • Sorry this time ist showing error that :-Error:(18, 15) error: cannot find symbol variable context – GoGam Jul 24 '17 at 13:16