我得到了“只有创建 View 层次结构的原始线程才能触及它的 View ”。因为我在使用“onUtteranceCompleted”时使用了文本转语音,并在内部对 TextView 进行了一些调用。
这是我的一些代码:
public class MyActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
private TextView txtCurrentWord;
public void onCreate(Bundle savedInstanceState) {
...
this.txtCurrentWord = (TextView) findViewById(R.id.txtCurrentWord);
}
public void onUtteranceCompleted(String uttId) {
this.txtCurrentWord.setText("hello world");
}
}
有人知道如何避免这个错误吗?
谢谢
最佳答案
这里有一个可能适合您的解决方案:
private Handler viewHandler;
public void onCreate(Bundle savedInstanceState) {
...
viewHandler = new Handler();
...
...
public void onUtteranceCompleted(String uttId) {
Runnable run = new Runnable() {
public void run() {
txtCurrentWord.setText("hello world");
}
};
viewHandler.post(run);
}
因此您保证您的 View 被原始线程所触及。
关于java - 文字转语音时出错 : "Only the original thread that created a view hierarchy can touch its views.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9353326/