1

Hi I wrote this code, and I dont understand why give me syntax error on token "}", delet this token?

private class DemoView extends View{
        public DemoView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }//here***

        final int x = 0;
        final int y = 0;

this.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View v, MotionEvent e){
                switch(e.getAction()){
                case MotionEvent.ACTION_DOWN:
                    x++;
                    break;
             case MotionEvent.ACTION_MOVE:   // touch drag with the ball
                // move the balls the same as the finger
                    x = x-25;
                    y = y-25;   
                    break;
                }
                return true;
            }//here***
         }   

Thanks

2 Answers 2

2

Multiple errors:

  1. First closing curly brace closed the constructor. Should be at the end of code.
  2. setOnTouchListener() missed the closing brace.
  3. You variables x,y should be fields (instead of automatic variables) so that they can be changed inside anonymous class View.OnTouchListener

Here is the corrected code (I hope it does what you intended):

public class DemoView extends View {

    int x = 0;
    int y = 0;

    public DemoView(Context context) {
        super(context);


        this.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent e) {
                switch (e.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x++;
                        break;
                    case MotionEvent.ACTION_MOVE:   // touch drag with the ball
                        // move the balls the same as the finger
                        x = x - 25;
                        y = y - 25;
                        break;
                }
                return true;
            }//here***
        });
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Edited the code to use fields instead a automatic variable of type MoveData.
0

You have forgotten one } at the end of the file. Also the statements after the declaration of two fields aren't contained in any method. You should move them to the constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.