简体   繁体   English

执行网络进程时无法在未调用Looper.prepare()的线程内创建处理程序

[英]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.

相关问题 更新数据库时无法在未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() while updating Database 无法在未调用 Looper.prepare() 的线程内创建处理程序,同时实现 IdlingResource - Can't create handler inside thread that has not called Looper.prepare(), while implementing IdlingResource 无法在尚未调用Looper.prepare()的线程内创建处理程序。 将文件上传到DropBox时的RuntimeExcetion - Can't create handler inside thread that has not called Looper.prepare(). RuntimeExcetion while uploading file to DropBox 发出Toast消息时出错:无法在未调用Looper.prepare()的线程内创建处理程序 - Error while dispaying an Toast message: Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()的线程内创建处理程序 - Can not create handler inside thread that has not called Looper.prepare() 无法在Handler()内未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside Handler() Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Handler或runOnUiThread解决方案“无法在未调用Looper.prepare()的线程内创建处理程序” - Handler or runOnUiThread solution for “Can't create handler inside thread that has not called Looper.prepare() ” Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM