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