简体   繁体   English

使用 Android 的线程错误“无法在尚未调用 Looper.Prepare 的线程内创建处理程序”

[英]Threading error “Can't create handler inside thread that has not called Looper.Prepare” with Android

I am currently developing an Android app where I need to perform a method inside a thread.我目前正在开发一个 Android 应用程序,我需要在其中执行一个线程内的方法。 I have the following code in order to create the thread and perform the我有以下代码来创建线程并执行

new Thread(new Runnable() { 
      @Override
      public void run() {
          new DownloadSync(getApplicationContext());
      }
}).start();

When I try to run this code it displays the error message in the log cat:当我尝试运行此代码时,它会在日志 cat 中显示错误消息:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.Prepare(). java.lang.RuntimeException:无法在未调用 Looper.Prepare() 的线程内创建处理程序。

How can I fix this problem?我该如何解决这个问题?

Use AsyncTask .使用AsyncTask Your situation is the reason for its existence.你的处境是它存在的理由。 Also, read up on Painless Threading (which is mostly using AsyncTask).另外,阅读无痛线程(主要使用 AsyncTask)。

The issue with your code is that something is trying to use a Handler internally in your new Thread, but nothing has told the Looper to prepare.您的代码的问题是某些东西试图在您的新线程内部使用Handler程序,但没有任何东西告诉Looper准备。

I had an earlier answer that was not very helpful - I've removed it and kept the relevant bit:我有一个较早的答案不是很有帮助-我已将其删除并保留了相关位:

Make sure your DownLoadSync object only communicates with the main UI thread via a handler (if you want to avoid using an AsyncTask) Declare a runnable and a handler as shown:确保您的 DownLoadSync object 仅通过处理程序与主 UI 线程通信(如果您想避免使用 AsyncTask)声明一个可运行和一个处理程序,如下所示:

final Runnable updateMainUIThread = new Runnable() {
        public void run() {
            Toast.makeText(getBaseContext(), "Communicating with the main thread!!", Toast.LENGTH_LONG).show();
        }
    };


private final Handler handler = new Handler();`

And use it like this: handler.post(updateMainUIThread);并像这样使用它: handler.post(updateMainUIThread);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() Android等待进度条无法在未调用Looper.prepare()错误的线程内创建处理程序 - Android Waiting progress bar Can't create handler inside thread that has not called Looper.prepare() error Android Studio错误:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android Studio Error: Can't create handler inside thread that has not called Looper.prepare() Unity to Android插件错误:无法在未调用Looper.prepare()的线程内创建处理程序 - Unity to Android plugin error: Can't create handler inside thread that has not called Looper.prepare() Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android Android无法在未调用looper.prepare()的线程内创建处理程序 - Android can't create handler inside thread that has not called looper.prepare() Android AsyncTask [无法在未调用Looper.prepare()的线程内创建处理程序 - Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()] java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android Android致命异常:无法在未调用Looper.prepare()的线程内创建处理程序 - Android fatal exception: Can't create handler inside thread that has not called Looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM