>我有一个 if 语句,用于检查三个插槽是否完成。如果是,计时器应该停止,但由于某种原因代码未运行。我看过一个类似于这个 If 条件的帖子,尽管条件正确,但永远不会执行,但是他们的解决方案什么也没解决。
这是我的代码:
停止功能
public void stop(ImageSwitcher slot){
slotOneFinished = (slot.equals(slotOne));
slotTwoFinished = (slot.equals(slotTwo));
slotThreeFinished = (slot.equals(slotThree));
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished){
//not running
Toast.makeText(MainActivity.this, "Running",Toast.LENGTH_SHORT).show();
checkWin(getFruits());
timer.cancel();
timer = null;
}
}
定时器
private Timer timer;
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new TimerTask() {
@Override
public void run() {
if (!slotOneFinished){
animate(randomSwitchCount(), slotOne);
}
if (!slotTwoFinished) {
animate(randomSwitchCount(), slotTwo);
}
if (!slotThreeFinished) {
animate(randomSwitchCount(), slotThree);
}
}
});
}
};
动画功能
public void animate(final int maxCount, final ImageSwitcher slot) {
i++;
if (i<maxCount){
Animation in = AnimationUtils.loadAnimation(this, R.anim.new_slot_item_in);
Animation out = AnimationUtils.loadAnimation(this, R.anim.old_item_out);
slot.setInAnimation(in);
slot.setOutAnimation(out);
int fruit = randomFruit();
slot.setTag(fruit);
slot.setImageResource(fruit);
}else {
stop(slot);
}
}
使用 == 也没有任何作用。
感谢您的帮助,
皮内特
这个条件永远不会成立,假设equals()
是以规范方式实现的,并且slotOne
、slotTwo
和slotThree
是3个不同的对象:
if (slotOneFinished&&slotTwoFinished&&slotThreeFinished)
看起来您对变量的范围有一个错误的假设。您可以通过使用这样的条件来解决此问题,改为:
if( slot == slotOne )
slotOneFinished = true;
。等等。
Android Studio 调试器是你的朋友。