Android,java定时器只工作一次
我试图制作一个计时器,每2秒向一个变量添加1。所以我使用Java定时器,但问题是,它只在第一次工作,这意味着它只向变量添加1 代码如下:Android,java定时器只工作一次,java,android,timer,Java,Android,Timer,我试图制作一个计时器,每2秒向一个变量添加1。所以我使用Java定时器,但问题是,它只在第一次工作,这意味着它只向变量添加1 代码如下: private Ball[] arr ; // ball array private int ballNum; // the number of the balls on the screen private float radius; // the radius of the balls private int count;// count if to sl
private Ball[] arr ; // ball array
private int ballNum; // the number of the balls on the screen
private float radius; // the radius of the balls
private int count;// count if to sleep or not
private Timer timer;
public BallCollection(int ballNum) {
this.arr = new Ball [ballNum];
this.ballNum = ballNum;
this.radius = 50;
this.count = 0;
timer = new Timer();
timer.scheduleAtFixedRate(new PachuTask(), 0, 2000);
for(int i = 0; i < arr.length; i++)
this.arr[i] = new Ball(this.radius);
}
private boolean isBumpWithRest(Ball[] few, Ball one) {
for(int i =0;i < this.arr.length; i++) {
if(this.arr[i] != one)
if(this.arr[i].isBump(one))
return true;
}
return false;
}
public void setBalls(Canvas canvas) {
Random rnd = new Random();
for(int i = 0; i < this.ballNum; i++) {
int x = (int) (rnd.nextInt((int)(canvas.getWidth() - 4 * this.radius)) + 2 * this.radius);
int y = (int) (rnd.nextInt((int)(canvas.getHeight() - 4 * this.radius)) + 2 * this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
while(this.isBumpWithRest(this.arr, this.arr[i]) && arr[i].getX() != 0) {
x = (int) (rnd.nextInt((int)(canvas.getWidth() - 2 * this.radius)) + this.radius);
y = (int) (rnd.nextInt((int)(canvas.getHeight() - 2 * this.radius)) + this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
}
}
}
public void draw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
p.setTextSize(50);
for (int i = 0; i < this.count; i++) {
arr[i].draw(canvas, p);
canvas.drawText("" + this.count, 100, 100, p);
}
}
class PachuTask extends TimerTask {
public void run() {
if(count < arr.length + 1)
count++;
}
}
private Ball[]arr;//球阵
私有int-ballNum;//屏幕上的球数
专用浮动半径;//球的半径
私有整数计数;//数一数是否睡觉
私人定时器;
公共BallCollection(int ballNum){
this.arr=新球[ballNum];
this.ballNum=ballNum;
这个半径=50;
此值为0.count;
定时器=新定时器();
timer.scheduleAtFixedRate(newpachutask(),020000);
对于(int i=0;i
尝试使用处理程序来实现这一点,它本身每隔2秒就会调用一次
Runnable runnable1 ,runnable2 = null ;
final Handler handler1 = new Handler();
final Handler handler2 = new Handler();
runnable2 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
handler1.postAtTime(runnable1, 0);
}
};
runnable1 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.err.println("Called in two second");
handler2.postDelayed(runnable2, 2000);
}
};
handler1.postDelayed(runnable1, 0);
尝试使用此代码
private Ball[] arr; // ball array
private int ballNum; // the number of the balls on the screen
private float radius; // the radius of the balls
private int count;// count if to sleep or not
private Timer timer;
public BallCollection(int ballNum) {
//check the value of ballNum here.
this.arr = new Ball[ballNum];
this.ballNum = ballNum;
this.radius = 50;
this.count = 0;
for (int i = 0; i < arr.length; i++) {
this.arr[i] = new Ball(this.radius);
}
//check the array size of arr here.
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//if (count < arr.length + 1) {
count = count + 1;
//}
System.out.println("" + count);
}
}, 0, 2000);
}
private boolean isBumpWithRest(Ball[] few, Ball one) {
for (int i = 0; i < this.arr.length; i++) {
if (this.arr[i] != one) {
if (this.arr[i].isBump(one)) {
return true;
}
}
}
return false;
}
public void setBalls(Canvas canvas) {
Random rnd = new Random();
for (int i = 0; i < this.ballNum; i++) {
int x = (int) (rnd.nextInt((int) (canvas.getWidth() - 4 * this.radius)) + 2 * this.radius);
int y = (int) (rnd.nextInt((int) (canvas.getHeight() - 4 * this.radius)) + 2 * this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
while (this.isBumpWithRest(this.arr, this.arr[i]) && arr[i].getX() != 0) {
x = (int) (rnd.nextInt((int) (canvas.getWidth() - 2 * this.radius)) + this.radius);
y = (int) (rnd.nextInt((int) (canvas.getHeight() - 2 * this.radius)) + this.radius);
this.arr[i].setX(x);
this.arr[i].setY(y);
}
}
}
public void draw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
p.setTextSize(50);
for (int i = 0; i < this.count; i++) {
arr[i].draw(canvas, p);
canvas.drawText("" + this.count, 100, 100, p);
}
}
private Ball[]arr;//球阵
私有int-ballNum;//屏幕上的球数
专用浮动半径;//球的半径
私有整数计数;//数一数是否睡觉
私人定时器;
公共BallCollection(int ballNum){
//在这里检查ballNum的值。
this.arr=新球[ballNum];
this.ballNum=ballNum;
这个半径=50;
此值为0.count;
对于(int i=0;i
您不需要创建(扩展)PachuTask
类
请根据您的需要修改它可能是因为它没有在
TimerTask
中输入if
条件?这不是因为我也尝试过这样做(if count<100),并且count变量始终保持为1。您是否尝试在if
中放置断点,并检查它是否每2秒到达一次?嗯,我真的不知道,因为我的朋友告诉我怎么做,但他说它会在计时器达到2秒后调用run Method,我想应该是这样的。您应该学习如何使用调试器。在这种情况下,每2秒钟检查一下它是否真的在输入run
方法,这确实可以帮助您。你用的是什么IDE?当然,有一些不错的教程教您如何调试程序。嗯,它不起作用,计数器变量的值保持为0。检查ballNum的值是否等于1,值是否为2,每次都尝试将其更改为10、50、100相同的结果。@user3674127检查数组是否已完全初始化,我又更改了代码嗯,你能告诉我如何在代码中使用这个吗?我不明白怎么做;将使用runnable2方法