2011-02-27 188 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

你已經忘記一個}在文件的結尾。另外兩個字段聲明後的聲明不包含在任何方法中。你應該將它們移到構造函數中。