2011-02-27 168 views
1

嗨我写了这段代码,我不明白为什么给我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*** 
     } 

感谢

回答

2

多个错误:

  1. 第一关闭大括号封闭的构造。应该在代码的最后。
  2. ​​错过了大括号。
  3. 你变量x,y应该是场(而不是自动变量),这样他们可以匿名类内部被改变View.OnTouchListener

以下是更正代码(我希望它你打算什么):

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*** 
     }); 
    } 
} 
+0

编辑代码以使用字段代替'MoveData'类型的自动变量。 – 2011-02-27 21:28:05

0

你已经忘记一个}在文件的结尾。另外两个字段声明后的声明不包含在任何方法中。你应该将它们移到构造函数中。

相关问题