[英]Can't create handler inside thread that has not called Looper.prepare() while executing Network process
I am attempting to run a networking process in the background of my app, to send an email. 我试图在我的应用程序后台运行网络过程,以发送电子邮件。 Since it's a networking task, it has to be run on a separate thread in the background.
由于这是一项网络任务,因此必须在后台的单独线程上运行。 However, I am unable to get the thread to execute properly.
但是,我无法使线程正确执行。 To start a new thread, I just create one, and run it using Thread.start(),
要启动一个新线程,我只需创建一个线程,然后使用Thread.start()运行它,
new Thread(){
public void run(){
Mail mail = new Mail();
mail.setFrom("someone@example.com");
mail.setTo("someone@to.com");
mail.setSubject("Hello");
mail.setMessage("World");
try
{
if (mail.send())
{
Toast.makeText(getActivity(),
"Email Sent Successfully", Toast.LENGTH_LONG)
.show();
}
else
Toast.makeText(getActivity(),
"Email Not Sent", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Log.i("Debug","Could not send email - " + e);
Log.i("Debug",e.getStackTrace().toString());
}
}
}.start();
However, I always get the error Can't create handler inside thread that has not called Looper.prepare() 但是,我总是收到错误消息:无法在尚未调用Looper.prepare()的线程内创建处理程序
I tried a few other things, trying to pass Looper.getMainLooper()
to the thread, via a Handler with Runnable, but it gives me the same error. 我尝试了其他一些尝试,试图通过带有Runnable的Handler将
Looper.getMainLooper()
传递给线程,但这给了我同样的错误。 I have also tried to use AsyncTask but it seems that since this is not a UI operation, use of AsyncTask is incorrect (plus, it also gives me the same error). 我也尝试过使用AsyncTask,但是由于这不是UI操作,因此AsyncTask的使用是不正确的(此外,它也给我带来同样的错误)。
Here's my stacktrace 这是我的堆栈跟踪
03-01 16:14:43.305: I/GreenBook(4454): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-01 16:14:43.305: I/GreenBook(4454): at android.os.Handler.<init>(Handler.java:121)
03-01 16:14:43.305: I/GreenBook(4454): at android.widget.Toast$TN.<init>(Toast.java:361)
03-01 16:14:43.305: I/GreenBook(4454): at android.widget.Toast.<init>(Toast.java:97)
03-01 16:14:43.305: I/GreenBook(4454): at android.widget.Toast.makeText(Toast.java:254)
03-01 16:14:43.305: I/GreenBook(4454): at com.stacksmashers.greenbook.RegisterActivity$10.run(RegisterActivity.java:688)
Can't create handler inside thread that has not called Looper.prepare()
无法在未调用Looper.prepare()的线程内创建处理程序
That is because you are trying to show a Toast
. 那是因为您要表演
Toast
。
I have also tried to use AsyncTask but it seems that since this is not a UI operation, use of AsyncTask is incorrect
我也尝试过使用AsyncTask,但是由于这不是UI操作,因此AsyncTask的使用不正确
A Toast
would be considered by most developers to be part of the UI. 大多数开发人员都将
Toast
视为UI的一部分。
plus, it also gives me the same error
另外,它也给我同样的错误
Perhaps you are trying to show the Toast
from doInBackground()
. 也许您正在尝试从
doInBackground()
展示Toast
。 Show your Toast
in onPostExecute()
(or onProgressUpdate()
, if you elect to call publishProgress()
). 如果您选择调用
publishProgress()
,请在onPostExecute()
(或onProgressUpdate()
显示您的Toast
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.