Android Thread/Handler Error IllegalStateException: The specified message queue synchronization barrier token has not been posted

假装没事ソ 提交于 2019-12-05 20:29:05

The problem is the use of Message object. It is a transient object, so once it has been sent to the Handler, the background thread should no longer use it. The receiving thread "owns" it at that point. Change you background thread to do something like this:

for (int i = 0; i < 11; i++) {
    Message msg = mHandler.obtainMessage(MY_MESSAGE_ID, i, 0);
    msg.sendToTarget();

    //  Delay or otherwise wait
}

On your UI thread's handler, you can then get the message and process it:

@Override
public void handleMessage(Message message) {
    switch (message.what) {
        case MY_MESSAGE_ID:
            repeat.setText("Repeat: " + message.arg1);
            break;

        default:
            Log.w(TAG, "Invalid message received: " + message.what);
            break;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!