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()
错过了右大括号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***
});
}
}