Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么会出现语法错误?_Android - Fatal编程技术网

Android 为什么会出现语法错误?

Android 为什么会出现语法错误?,android,Android,嗨,我写了这段代码,我不明白为什么给我标记“}上的语法错误,删除这个标记 private class DemoView extends View{ public DemoView(Context context) { super(context); // TODO Auto-generated constructor stub }//here*** final int x = 0;

嗨,我写了这段代码,我不明白为什么给我
标记“}上的语法错误,删除这个标记

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***
         }   

谢谢

您忘记了文件末尾的一个
}
。另外,两个字段声明后的语句不包含在任何方法中。您应该将它们移动到构造函数。

多个错误:

  • 第一个关闭的花括号关闭了构造函数。应该在代码的末尾
  • setOnTouchListener()
    错过了右大括号
  • 变量x,y应该是字段(而不是自动变量),这样它们就可以在匿名类
    View.OnTouchListener
  • 下面是正确的代码(我希望它能达到您的目的):


    编辑代码以使用字段,而不是类型为
    MoveData
    的自动变量。
    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***
            });
        }
    }