简体   繁体   English

Android Handler在Eclipse中被Looper反复调用,而不是在使用后Looper丢弃消息

[英]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: 检查要点:

  1. activity.doSomething() will not send message GameMessageHandler.DO_SOMETHING activity.doSomething()将不会发送消息GameMessageHandler.DO_SOMETHING
  2. 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.

相关问题 PhoneGap android:无法在未调用Looper.prepare()的线程内创建处理程序 - PhoneGap android: getting cannot create handler inside thread that has not called Looper.prepare() Android Activity是否会关闭Looper和Handler? - Android Activity will close Looper & Handler? 使用Thread / Handler / Looper for Worker Thread Android - Using Thread/Handler/Looper for Worker Thread Android Android:looper / handler与Java Observer? - Android: looper/handler vs. Java Observer? 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android 警报对话框崩溃Android测试 - “无法在未调用Looper.prepare()的线程内创建处理程序” - Alert Dialog crashes Android test - “Can't create handler inside thread that has not called Looper.prepare()” Android上的Junit测试领域。 无法在未调用Looper.prepare()的线程内创建处理程序 - Junit Testing Realm on Android. Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()的线程内创建处理程序-Android Marmalade - Can't create handler inside thread that has not called Looper.prepare() - Android Marmalade 无法在未调用Looper.prepare()Android的线程内创建处理程序。 循环计时器 - Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM