我需要从处理线程制作 Android Toast,这是 OpenCV 自定义的,所以我不能按照此处的建议使用 runOnUiThread(): Android: Toast in a thread。
大部分代码来自 CVCamera 示例应用程序。但是那些不熟悉的,当我选择 Surf 菜单按钮时,SURFProcessor 是这样调用的:
else if (item.getTitle().equals("SURF")) {
defaultcallbackstack.addFirst(new SURFProcessor());
toasts(DIALOG_TUTORIAL_SURF, "");
}
运行此处理器线程,以便当我按下手机的相机按钮 (capturePress = true) 时,会拍摄图像并完成处理。我想调用 toasts 方法,如图所示:
class SURFProcessor implements NativeProcessor.PoolCallback {
@Override
public void process(int idx, image_pool pool, long timestamp,
NativeProcessor nativeProcessor) {
if(capturePress) {
String processMsg = processor.processFeatures(idx, pool, cvcamera.DETECT_SURF);
capturePress = false;
toasts(PROCESS_MESSAGE, processMsg);
}
}
}
这是 toasts 方法,位于扩展 Activity 的主类中:
void toasts(int id, String msg) {
switch (id) {
case PROCESS_MESSAGE:
Toast.makeText(MMRapp.this, msg, Toast.LENGTH_LONG).show();
break;
.........
现在这段代码给了我一个错误:“不能在没有调用 Looper.prepare() 的线程内创建处理程序。” 我该如何调用 toasts 方法?或者是否可以让 toasts 方法监听 processMsg 的变化?如果可能的话,我可以通过发送 processMsg 或更改类变量来代替。本质上,我需要从这个处理器线程更新一个字符串。
非常感谢,如果需要,我会提供更多信息/代码。
-汤姆