我收到错误" android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图"当我在下面的代码中调用listenButton.setAlpha((float)1)
时。我理解为什么,但是当我收到onDone事件时如何修改按钮呢?
public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener {
[...]
@Override
// OnInitListener method to receive the TTS engine status
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsOK = true;
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
Button listenButton = (Button) findViewById(R.id.listentts);
listenButton.setAlpha((float)1);
listenButton.setClickable(true);
}
@Override
public void onError(String utteranceId) {
Log.d("MainActivity", "Progress on Error " + utteranceId);
}
@Override
public void onStart(String utteranceId) {
Log.d("MainActivity", "Progress on Start " + utteranceId);
}
});
}
else {
ttsOK = false;
}
}
[...]
}
答案 0 :(得分:1)
试试这个:
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Button listenButton = (Button) findViewById(R.id.listentts);
listenButton.setAlpha((float)1);
listenButton.setClickable(true);
}
});
答案 1 :(得分:1)
每个View
都有Handler
与之关联,因此无需创建新的。{/ p>
final View v = findViewById(R.id.listentts); // could be a class member
v.getHandler().post(new Runnable() {
@Override
public void run() {
v.setAlpha(1.0f);
v.setClickable(true);
}
});