java Android线程/处理程序错误IllegalStateException:尚未发布指定的消息队列同步屏障令牌
我想做的
使用处理程序和后台线程,每3秒x 10次在UI线程上更新TextView。用户界面上的输出应该是“Repeat:1”,并且每3秒输出一次,然后输入一个数字。例如,“重复:1”在3秒钟后更新为“重复:2”,然后在3秒钟后更新为“重复:3”
我是如何做到这一点的
我测试的第一种方法是使用for循环和线程。sleep()使每个循环延迟n秒。在每个循环中,我调用sendMessage(message)方法,我的理论是每次都会调用UI线程上的handleMessage()方法。以下是代码:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TextView repeat;
private Handler mHandler;
private Thread backgroundThread;
private String uiString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate(Bundle) called by Gil ");
repeat = (TextView)findViewById(R.id.view_repeat);
runInBackground();
backgroundThread.start();
mHandler = new Handler() {
@Override
public void handleMessage(Message message) {
Bundle bundle = message.getData();
uiString = bundle.getString("count");
repeat.setText("Repeat: " + uiString);
}
};
}
public void runInBackground() {
Log.d(TAG, "runInBackGround() called by Gil");
backgroundThread = new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Background Thread started by Gil");
Bundle bundle = new Bundle();
Message message = new Message();
for (int i = 1; i < 11; i++) {
bundle.putString("count", ""+i);
message.setData(bundle);
mHandler.sendMessage(message);
try {
// delay for 3 seconds
Thread.sleep(3000);
} catch (Throwable t) {
Log.d(TAG, "Throwable Error caused by Thread.Sleep() & Gil");
}
}
}
});
}
}
我跑步时的结果
它会更新一次:“重复:1”->;“重复:2”,然后应用程序自行停止/退出。错误堆栈跟踪如下所示:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: personal.development.gilho.timerstuff, PID: 29226
java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.
at 安卓.os.MessageQueue.removeSyncBarrier(MessageQueue.java:289)
at 安卓.os.Looper.removeSyncBarrier(Looper.java:316)
at 安卓.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at 安卓.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6521)
at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at 安卓.view.Choreographer.doCallbacks(Choreographer.java:613)
at 安卓.view.Choreographer.doFrame(Choreographer.java:583)
at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at 安卓.os.Handler.handleCallback(Handler.java:733)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:146)
at 安卓.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
我到目前为止所做的
- 这个SO Question:建议使用
dispatchMessage()
而不是sendMessage()
。尝试过之后,同样的错误也发生了李> - 这SO Question:与我的问题非常相似,但没有答案,评论建议尝试一本关于游戏开发的书李>
- 这个SO Question:似乎是另一个问题,因为这里的问题与回收消息有关,而这里没有回收李>
- 这SO Question:结果类似,但没有重复尝试持续更新处理程序。答案也不适用于我的情况李>
我怀疑在每个循环中使用Thread.sleep()
方法的for循环是我试图实现的一个愚蠢的实现。我的下一个选择是尝试使用倒计时,处理程序现有的延迟函数,如sendMessageDelayed()
或postDelayed()
。最后,我将尝试使用Timer
但就目前而言,在我尝试其他解决方案之前,我想帮助理解为什么这不起作用。我无法使用stacktrace中提供的信息成功识别出哪里出了问题,也无法在调试模式下查看每一行
# 1 楼答案
问题在于
Message
对象的使用。它是一个临时对象,因此一旦它被发送到Handler
,后台线程就不应该再使用它。接收线程在该点“拥有”它。更改背景线程以执行以下操作:在UI线程的处理程序上,您可以获取消息并对其进行处理: