简体   繁体   English

java.lang.RuntimeException:无法在尚未调用 Looper.prepare() 的线程内创建处理程序错误

[英]java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() Error

SongList Code in the image Not able to understand what actually the error is and why it is caused.Error shown shown below.图像中的歌曲列表代码无法理解错误究竟是什么以及为什么会导致错误。错误如下所示。

W/System.err: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
W/System.err:     at android.os.Handler.<init>(Handler.java:200)
W/System.err:     at android.os.Handler.<init>(Handler.java:114)
W/System.err:     at android.app.Activity.<init>(Activity.java:754)
W/System.err:     at android.app.ListActivity.<init>(ListActivity.java:175)
W/System.err:     at com.example.vipul.finalproject.PlayListActivity.<init>(PlayListActivity.java:0)
W/System.err:     at com.example.vipul.finalproject.SongList.scanSongs(SongList.java:296)
W/System.err:     at com.example.vipul.finalproject.activities.ActivityMenuMain$ScanSongs.doInBackground(ActivityMenuMain.java:299)
W/System.err:     at com.example.vipul.finalproject.activities.ActivityMenuMain$ScanSongs.doInBackground(ActivityMenuMain.java:289)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
W/System.err:     at java.lang.Thread.run(Thread.java:818)

See this article to understand the whole Looper / Handler relationship.这篇文章了解整个Looper / Handler关系。

Long story short, your Thread#run() method must follow a structure like so:长话短说,您的Thread#run()方法必须遵循如下结构:

new Thread(new Runnable() {
    @Override
    public void run() {
        Looper.prepare();
        mHandler = new Handler();
        Looper.loop();
    }
}).start();

The best explanation on why you need a looper on one thread to save instructions and another thread to execute them.关于为什么需要一个线程上的循环器来保存指令和另一个线程来执行它们的最佳解释 In a multi thread application task may come while executing other task.在多线程应用程序中,任务可能会在执行其他任务时到来。

暂无
暂无

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

相关问题 java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: 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 无法在未调用Looper.prepare()的线程内创建处理程序:java.lang.RuntimeException - Can't create handler inside thread that has not called Looper.prepare() : java.lang.RuntimeException java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序吗? - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() WHY? java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 异步任务:java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Async Task: java.lang.RuntimeException: 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() Java无法在未调用Looper.prepare()的线程内创建处理程序 - Java Can't create handler inside thread that has not called Looper.prepare() 派生的TimerTask类中的错误:无法在未调用Looper.prepare()的线程内创建处理程序 - Error in derived TimerTask class: `Can't create handler inside thread that has not called Looper.prepare()`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM