不管鸿蒙还是安卓都要求在UI线程更新UI相关组件,在相关项目时使用TaskDispatcher执行识别任务,并识别出结果
TaskDispatcher taskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
taskDispatcher.syncDispatch(() -> {
//获取识别结果
ArrayList<Recognition> results = mPredictor.recognize("person");
//之后在Layout上绘制识别结果
rootContainer.addDrawTask(new Component.DrawTask() {//在此处会报一下错误,见下表
@Override
public void onDraw(Component component, Canvas canvas) {
//省略一部分代码...
canvas.drawRect(location.left, location.top, location.right, location.bottom, rectPaint);
}
});
}
由于没有在UI线程更新代码报错如下
E AndroidRuntime: FATAL EXCEPTION: PoolThread-16
E AndroidRuntime: Process: com.wincent.harmonybot, PID: 12921
E AndroidRuntime: java.lang.IllegalStateException: Attempt to update UI in non-UI thread.
E AndroidRuntime: at ohos.agp.components.Component.nativeAddDrawTaskOverContent(Native Method)
E An