0

MediaPipe在 Android 中使用从图表中获取输出,并将该输出设置为EditText文本。以下是代码:

editText.setText("hello world".toString());
 processor.addPacketCallback(
          OUTPUT_STREAM_NAME,
          (packet) -> {
              String output = PacketGetter.getString(packet);
                  editText.setText("hello world 1".toString());
                  //editText.setText(output.toString());
              Log.d(TAG,"[TS:" + packet.getTimestamp() + " : " + ouput);
          });

回调外部工作正常,但回调内部setText()从未设置,每当我尝试选择EditText以下异常时:

java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
4

1 回答 1

0

因此,事实证明,它addPacketCallback()调用了一个工作线程,并且在那里更改 UI 需要在 UI 线程上显式运行它

editText.setText("hello world".toString());
processor.addPacketCallback(
        OUTPUT_STREAM_NAME,
        (packet) -> {
            String output = PacketGetter.getString(packet);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    editText.setText("hello world 1");
                }
            });
            //editText.setText(output.toString());
            Log.d(TAG, "[TS:" + packet.getTimestamp() + " : " + ouput);
        });
于 2021-07-01T14:36:28.480 回答