Error while sending email

Asked

Viewed 52 times

-1

I have this error when sending email that occurs right in this code snippet. Basically it’s a form that receives two pieces of information and sends it to a predefined email.

java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
        at android.widget.Toast$TN.<init>(Toast.java:936)
        at android.widget.Toast.<init>(Toast.java:205)
        at android.widget.Toast.makeText(Toast.java:627)
        at android.widget.Toast.makeText(Toast.java:588)
        at br.com.cleiton.envioemail.MainActivity$2.run(MainActivity.java:69)
        at java.lang.Thread.run(Thread.java:764)
new Thread(new Runnable(){
@Override
public void run() {
    Mail m = new Mail();

    String[] toArr = {email};
    m.setTo(toArr);


    m.setSubject(subject);
    m.setBody(body);

    try {
        m.send();
        } catch(RuntimeException rex) { 

        } catch(Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        Toast.makeText(getApplicationContext(), "Email enviado!", Toast.LENGTH_SHORT).show();
    }
}).start();
  • What is the original error message? Post a stackTrace tbm, pf

1 answer

0

The error is that you are invoking a UI function on a thread that is not the main one. Try surrounding this call to Toaster as follows:

runOnUiThread(new Runnable() {
     @Override
     public void run() {
         Toast.makeText(getApplicationContext(), 
              "Email enviado!", 
              Toast.LENGTH_SHORT)
         .show();
     }
});
  • show, it worked ...thanks by force.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.