[英]Android Handler getting called repeatedly by Looper in Eclipse instead of Looper discarding message after use
I think I may have accidentally switched on some setting I didn't intent to in Eclipse or something. 我想我可能不小心打开了一些我不打算在Eclipse中使用的设置。 I'm working on an android game and from within the game logic, when certain events happen I send messages to the main activity to display certain things.
我正在开发一个android游戏,并且在游戏逻辑中,当发生某些事件时,我会向主要活动发送消息以显示某些东西。
I have the following classes: 我有以下课程:
public class GameMessageHandler extends Handler {
public static final int DO_SOMETHING = 0;
private final WeakReference<MyActivity> myActivity;
public GameMessageHandler(MyActivity myActivity) {
this.myActivity = new WeakReference<MyActivity>(myActivity);
}
@Override
public void handleMessage(Message msg) {
if (myActivity != null) {
MyActivity activity = myActivity.get();
if (msg.what == DO_SOMETHING) {
//even if the caller only called this one time, this keeps executing forever
activity.doSomething();
}
}
}
}
public class GameEventListenerAndroid implements GameEventListener {
private Handler handler;
public GameEventListenerAndroid(GameMessageHandler gameMessageHandler){
this.handler = gameMessageHandler;
}
@Override
public void playerTookSomeAction() {
//this only gets called one time
handler.sendEmptyMessage(GameMessageHandler.DO_SOMETHING);
}
}
I KNOW this logic works because what I'm trying to run right now is from a backup I saved after I saw it work MULTIPLE times, but as I said what is happening right now is for some reason the Looper
keeps calling the message handler with the same message a million times per second and never discards the message to move on to something else. 我知道这种逻辑是可行的,因为我现在要运行的是备份,该备份是我多次查看后保存的,但是正如我所说的,由于某些原因,
Looper
一直使用同一条消息每秒百万次,并且永远不会丢弃该消息以继续前进。
Any help is greatly appreciated! 任何帮助是极大的赞赏!
Check points: 检查要点:
activity.doSomething()
will not send message GameMessageHandler.DO_SOMETHING
activity.doSomething()
将不会发送消息GameMessageHandler.DO_SOMETHING
playerTookSomeAction()
is not called repeatedly playerTookSomeAction()
不会重复调用 Just format it to be BETTER, incase someone feels so boring! 只要将其格式化为更好,以防有人感到无聊!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.