我得到了关于gettext()的问题,Android工作室说'方法getText必须从UI线程调用,当前推断的线程是工人。有人可以帮我这个还是有人可以给一个想法如何解决这一问题? 谢谢!Android Studio错误:“方法getText必须从UI线程中调用,当前推断的线程是工作者
package com.ibm.watsonvoicetts;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.ibm.watson.developer_cloud.android.library.audio.StreamPlayer;
import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech;
import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editText;
Button button;
StreamPlayer streamPlayer;
private TextToSpeech initTextToSpeechService(){
TextToSpeech service = new TextToSpeech();
String username = "";
String password = "";
service.setUsernameAndPassword(username, password);
return service;
}
private class WatsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... textToSpeak) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("running the Watson thread");
}
});
TextToSpeech textToSpeech = initTextToSpeechService();
streamPlayer = new StreamPlayer();
streamPlayer.playStream(textToSpeech.synthesize(
String.valueOf(editText.getText()),【**here is the problem**】
Voice.EN_MICHAEL).execute());
return "text to speech done";
}
@Override
protected void onPostExecute(String result) {
textView.setText("TTS status: " + result);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("the text to speech: " + editText.getText());
textView.setText("TTS: " + editText.getText());
WatsonTask task = new WatsonTask();
task.execute(new String[]{});
}
});
}
}
看看这个[问题](https://stackoverflow.com/questions/32011996/method-gettext-must-be-called-from-the-ui-thread-android-studio) –