-1

I have tried to run and compile into my phone this code:

public class SingleTouchEventView extends View {
  private Paint paint = new Paint();
  private Path path = new Path();

  public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      path.moveTo(eventX, eventY);
      return true;
    case MotionEvent.ACTION_MOVE:
      path.lineTo(eventX, eventY);
      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }
} 

but it crushes and my logcat says the following:

can't initiate class ...... no empty constructor

Any clue what i am doing wrong?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46

1 Answers1

1

May be you are trying to create an object as below...

SingleTouchEventView view = new SingleTouchEventView ();

But you doesn't have any empty constructor. If you want to solve the problem temporarily then create an empty Constructor in your SingleTouchEventView class as below...

public SingleTouchEventView() {

}

When you extends View class then you can't have empty constructor. So, the above constructor will through an error saying...

Implicit super constructor View() is undefined. Must explicitly invoke another constructor

So, you have to pass the Context at least through the constructor...Now, you should create another Constructor with Context as below...

public SingleTouchEventView(Context context) {
    super(context);
}

public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);

}

And when you will create an object of SingleTouchEventView class then you initialize as below...

SingleTouchEventView view = new SingleTouchEventView (getApplicationContext());
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • Also where should i put that constructor, cause it says: "implicit super constructor View() is undefined. Must explicitly invoke another constructor." when i throw it into my code. – We're All Mad Here Mar 19 '14 at 16:00
  • do not take me wrong. But having an empty constructor will lead to a compile time error: The implicitly View() constructor is undefined. Must explicitly invoke another constructor – Blackbelt Mar 19 '14 at 16:01
  • Yeah, but that's what it's all about what should i do? – We're All Mad Here Mar 19 '14 at 16:02
  • @We'reAllMadHere Paste the complete logcat of the execption – Blackbelt Mar 19 '14 at 16:08
  • @We'reAllMadHere there is chapter 4 Professional Android Devleopment By Retro Meier. Its has everything that you need to know about custom views – Raghunandan Mar 19 '14 at 16:08