【问题标题】:Android Studio error: "Method getText must be called from the UI Thread, currently inferred thread is workerAndroid Studio报错:“方法getText必须从UI线程调用,当前推断线程为worker
【发布时间】:2017-06-08 03:36:56
【问题描述】:

我遇到了关于 gettext() 的问题,Android 工作室说“方法 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[]{});


        }
    });
}

}

【问题讨论】:

标签: java android gettext


【解决方案1】:

您不应该像现在这样从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());

【讨论】:

    【解决方案2】:

    doInBackground() 方法在不同的线程上运行,而不是在 UI 线程中。

    streamPlayer.playStream(textToSpeech.synthesize(
                    String.valueOf(editText.getText()),【**here is the problem**】
                    Voice.EN_MICHAEL).execute());
    

    您正在此 doInBackground() 方法中调用 edittext 的 getText() 方法。这就是它给出错误的原因。任何与视图相关的工作,我们都必须在 UI 线程中完成。

    你可以做的一件事,你可以把这段代码放在runOnUiThread中。

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      相关资源
      最近更新 更多