Android-java.lang.runtimeexception:can ' t create handler inside thread that have not called looper.prepare ()

Source: Internet
Author: User

Chapter from: LUCHG Technical Exchange http://www.luchg.com All rights reserved. This site, in addition to the source, are the author of original articles, can be freely quoted, but please indicate the source, thank you.
Android-java.lang.runtimeexception:can ' t create handler inside thread that have not called looper.prepare ()

This exception is encountered when we use the Android UI in a new thread

Read a lot of articles are still not clear what the specific reasons, but the problem is very simple to deal with, the following code:

?
1234567891011 @Overridepublic void run() {                 //前面代码省略    Looper.prepare();    Log.i("---my tag---", "下载完成:"+downloadPath);    Toast.makeText(context, fileName+"下载完成...", Toast.LENGTH_LONG).show();    Looper.loop();            }

Add a line of Looper.prepare () before using the toast, and then add a row of Looper.loop () after the toast. Read a lot of articles do not understand, so here is not misleading everyone, interested in their own search reasons.

The above code is a part of my own file download function code, the full code can be viewed:

android-File Download

Article from: LUCHG Technical Exchange http://www.luchg.com All rights reserved. This site, in addition to the source, are the author of original articles, can be freely quoted, but please indicate the source, thank you.
Android-java.lang.runtimeexception:can ' t create handler inside thread that have not called looper.prepare ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.