2017-06-11 8 views
-1

こんにちはすべて私はAndroidに新しく、英語からドイツ語への翻訳アプリを開発していました。 : この問題の解決方法を教えてください。Androidエラー:メソッドgettextは、現在推測されているUIスレッドから呼び出す必要がありますスレッドは動作しています

package com.exmaple.android.lang_trans; 

import java.util.Locale; 

import android.app.Activity; 

import android.os.AsyncTask; 

import android.os.Bundle; 

import android.speech.tts.TextToSpeech; 

import android.speech.tts.TextToSpeech.OnInitListener; 

import android.util.Log; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.EditText; 

import android.widget.TextView; 

import com.exmaple.android.lang_trans.R; 

import com.memetix.mst.language.Language; 

import com.memetix.mst.translate.Translate; 

public class MainActivity extends Activity implements OnInitListener { 

    private TextToSpeech tts; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tts = new TextToSpeech(this, this); 
     ((Button) findViewById(R.id.bSpeak)).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       speakOut(((TextView) findViewById(R.id.tvTranslatedText)).getText().toString()); 
      } 
     }); 

     ((Button) findViewById(R.id.bTranslate)).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       class bgStuff extends AsyncTask<Void, Void, Void> { 

        String translatedText = ""; 

        @Override 
        protected Void doInBackground(Void... params) { 
         // TODO Auto-generated method stub 
         try { 
          //below line is throwing the error 
          String text = ((EditText) findViewById(R.id.etUserText)).getText().toString(); 
          translatedText = translate(text); 
         } catch (Exception e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
          translatedText = e.toString(); 
         } 

         return null; 
        } 

        @Override 
        protected void onPostExecute(Void result) { 
         // TODO Auto-generated method stub 
         ((TextView) findViewById(R.id.tvTranslatedText)).setText(translatedText); 
         super.onPostExecute(result); 
        } 

       } 

       new bgStuff().execute(); 
      } 
     }); 
    } 

    public String translate(String text) throws Exception { 


     // Set the Client ID/Client Secret once per JVM. It is set statically and applies to all services 
     Translate.setClientId("CLIENT ID"); //Change this 
     Translate.setClientSecret("CLIENT SECRET"); //change 


     String translatedText = ""; 

     translatedText = Translate.execute(text, Language.GERMAN); 

     return translatedText; 
    } 

    @Override 
    public void onInit(int status) { 
     // TODO Auto-generated method stub 
     if (status == TextToSpeech.SUCCESS) { 

      int result = tts.setLanguage(Locale.GERMAN); 

      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e("TTS", "This Language is not supported"); 
      } else { 

       //speakOut("Ich"); 
      } 

     } else { 
      Log.e("TTS", "Initilization Failed!"); 
     } 
    } 

    private void speakOut(String text) { 
     tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
    } 

} 

答えて

2

あなたの問題は、あなたがAndroidのシステムはあなたがあなたのonClick機能を変更することができ

you cannot update the UI from any thread other than the UI thread or the "main" thread.

https://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads

を許可していないUIスレッド以外のスレッドであなたのUI要素にアクセスしているということです

((Button) findViewById(R.id.bTranslate)).setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     class bgStuff extends AsyncTask<String, Void, String> { 
      @Override 
      protected String doInBackground(String... params) { 
       // TODO Auto-generated method stub 
       try { 
        if (params.length > 0) { 
         return translate(params[0]); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
        return e.toString(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       ((TextView) findViewById(R.id.tvTranslatedText)).setText(result); 
      } 
     } 

     new bgStuff().execute(((EditText) findViewById(R.id.etUserText)).getText().toString()); 
    } 
}); 
+0

こんにちは@Aravindaこの回答があなたの質問を解決した場合チェックマークをクリックして受け入れることを検討してください。これは、あなたが解決策を見つけ出し、回答者とあなた自身の両方に評判を与えていることを広範なコミュニティに示します。これを行う義務はありません。 –

関連する問題