![](/img/trans.png)
[英]Android Studio error: "Method getText() must be called from the UI Thread, currently inferred thread is worker
[英]Android Studio error: "Method getText must be called from the UI Thread, currently inferred thread is worker
我有一個關於gettext()的問題,Android Studio說“必須從UI線程調用方法getText,當前推斷的線程是worker”。 有人可以幫助我解決這個問題嗎? 謝謝!
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[]{});
}
});
}
}
您不應像這樣做那樣從AsyncTask
調用getText
。 解決方案是將值傳遞到任務的構造函數中。
更改
WatsonTask task = new WatsonTask(editText.getText());
和
private class WatsonTask extends AsyncTask<String, Void, String> {
private final String text;
public WatsonTask(String text) {
super();
this.text = text;
}
和
streamPlayer.playStream(textToSpeech.synthesize(
String.valueOf(text),
Voice.EN_MICHAEL).execute());
doInBackground()方法在UI線程中以外的其他線程上運行。
streamPlayer.playStream(textToSpeech.synthesize(
String.valueOf(editText.getText()),【**here is the problem**】
Voice.EN_MICHAEL).execute());
您正在此doInBackground()方法中調用edittext的getText()方法。 這就是為什么會出錯。 任何與視圖相關的工作,我們都必須在UI線程中完成。
您可以做的一件事,就是將這段代碼放在runOnUiThread中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.