-1

当我调用listenButton.setAlpha((float)1)下面的代码时,我收到错误“android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图”。我明白为什么,但是当我收到 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;
       }
    }
    [...]
}
4

2 回答 2

2

每个View都有一个Handler与之关联的,所以没有必要创建一个新的。

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);
    }
});
于 2015-10-18T20:22:05.200 回答
0

尝试这个:

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);
    }
});
于 2015-10-18T19:48:32.837 回答