从Android应用程序发送电子邮件

时间:2013-03-15 05:06:17

标签: android

我正在尝试从我的Android应用程序发送电子邮件来自background.Mail已成功发送附件但仍然在我之后收到日志错误。这是日志请指导我错误在哪里以及如何解决它。

03-14 13:37:37.697: E/MailApp(1296): Could not send email
03-14 13:37:37.697: E/MailApp(1296): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-14 13:37:37.697: E/MailApp(1296):    at android.os.Handler.<init>(Handler.java:197)
03-14 13:37:37.697: E/MailApp(1296):    at android.os.Handler.<init>(Handler.java:111)
03-14 13:37:37.697: E/MailApp(1296):    at android.widget.Toast$TN.<init>(Toast.java:324)
03-14 13:37:37.697: E/MailApp(1296):    at android.widget.Toast.<init>(Toast.java:91)
03-14 13:37:37.697: E/MailApp(1296):    at  android.widget.Toast.makeText(Toast.java:238)
03-14 13:37:37.697: E/MailApp(1296):    at com.example.mytest.ReadContactsActivity.SendMail(ReadContactsActivity.java:173)
03-14 13:37:37.697: E/MailApp(1296):    at com.example.mytest.ReadContactsActivity.access$0(ReadContactsActivity.java:156)
03-14 13:37:37.697: E/MailApp(1296):    at com.example.mytest.ReadContactsActivity$CountDownTask.doInBackground(ReadContactsActivity.java:131)
03-14 13:37:37.697: E/MailApp(1296):    at com.example.mytest.ReadContactsActivity$CountDownTask.doInBackground(ReadContactsActivity.java:1)
03-14 13:37:37.697: E/MailApp(1296):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-14 13:37:37.697: E/MailApp(1296):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-14 13:37:37.697: E/MailApp(1296):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-14 13:37:37.697: E/MailApp(1296):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-14 13:37:37.697: E/MailApp(1296):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

2 个答案:

答案 0 :(得分:1)

我认为您试图在Toast AsynTask方法中显示doInBackground()条消息, 只需在onPostExecute()方法中执行....

答案 1 :(得分:0)

你可能正在使用一些直接尝试在主线程中进行更改的后台线程,如果这是问题则使用

runOnUiThread(new Runnable() {
  public void run() {
    //do your work here
  }
});