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 ()