2017-02-23 43 views
-2

在該線下方它給我的錯誤說:得到錯誤 - 該方法的getText必須從UI線程調用,目前推斷線程是工人

方法的getText必須從UI線程調用,當前推斷的線程是工作者「。

如何解決該錯誤?

// Get the text from EditText 
String wordsToTranslate = **translateEditText.getText()**.toString(); 

大膽的部分是它告訴我存在錯誤的地方。任何關於如何解決這個問題的想法?

Screen shot of the error

+0

你不能做「doInBackgound」的方法UI相關操作 –

+0

你可以用UI(EditText上工作的一部分的UI)只能從UI線程。 'doInBackground'應該在後臺線程中調用。那就是問題所在。在'doInBackground'之外獲取文本,例如在'onPreExecute'上。 –

+0

感謝您的快速回復。我只需刪除該行代碼並將其放在覆蓋之前(意味着在doInBackground之外),並解決了問題。感謝所有的幫助。 :) – Mufasa

回答

0

嘗試這種情況:

字符串wordsToTranslate = translateEditText.getText()的toString();

new Translate()。execute(wordsToTranslate);

class Translate extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... args) { 
      // Building Parameters 
      String wordsToTranslate = args[0]; 


      //do your work 

    } 

} 
相關問題