1

Hello I'm trying to use google translate api to translate a string when I click Translate in the button. When I run as a AVD it works fine, the problem is when I try to deployment with my smartphone, when I click the translate button the app closses and I get an error.

Bundle bundle = getIntent().getExtras();
    String word = "";
    if(bundle.containsKey("Name")){
        word = (String) bundle.getString("Name");
        txtTarget.setText(word);
        //envia para o translate api para traduzir a palavra pro portugues
    }

    final String finalWord = word;
    btnResultado.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            final Handler textViewHandler = new Handler();

            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {

                    Translate translate = TranslateOptions.newBuilder().setApiKey(API_KEY).build().getService();

                    final Translation translation = translate.translate(finalWord, Translate.TranslateOption.targetLanguage("pt"));

                    textViewHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            txtSource.setText(translation.getTranslatedText());
                        }
                    });
                    return null;
                }
            }.execute();
        }
    });

Error:

Process: com.google.sample.cloudvision, PID: 8874 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:200) at android.os.Handler.(Handler.java:114) at android.widget.Toast$TN.(Toast.java:345) at android.widget.Toast.(Toast.java:101) at android.widget.Toast.makeText(Toast.java:259) at com.google.sample.cloudvision.ActSegundaTela$1$1$1.doInBackground(ActSegundaTela.java:82) at com.google.sample.cloudvision.ActSegundaTela$1$1$1.doInBackground(ActSegundaTela.java:79) at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

brunocdias
  • 13
  • 3

2 Answers2

0

You don't need to create a new Handler. You can use any view as a Handler.

txtSource.post(new Runnable() {
    public void run(){
        txtSource.setText(...
    }
});

If you really want to create a Handler, then as mentioned in the error message, call Looper.prepare(); in the first line of doInBackground and Looper.loop() in the last line.

doInBackground(){
    Looper.prepare();
    //your code
    Lopper.loop();
}

If your code is inside an activity or have the reference of activity, you can directly do stuffs in the UI thread using the method runOnUiThread

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

Not sure which line it is in particular, but from the Exception, it looks like one of the methods inside your doInBackground() method is trying to display a Toast. Trying to display Views from background threads is a no-no, which is what's causing the crash.

You could wrap the block in a try { } catch(Exception e) { ... } to identify the line that's trying to display the Toast -- since that piece should probably be pulled out of the background thread to prevent the Exception.

Submersed
  • 8,810
  • 2
  • 30
  • 38
  • There's no real need to debug this further; this is the classic result of trying to do UI operations on another thread. – Chris Stratton Oct 10 '17 at 03:26
  • I used Toast just to check where my code goes, I forget to clean and deploy again, but with toast I ckecked that it runs until this line "new AsyncTask() {" and stops – brunocdias Oct 10 '17 at 11:01