Android Development-New thread crashes

Source: Internet
Author: User

has been not satisfied with the car can not realize the screen saver, recently found by setting the brightness to achieve, gu initiation of their own program to realize the idea, then modify the original practiced hand app. After all, the past 1 years, all kinds of programs, the SDK has been updated to not know how many versions. Experienced a painful upgrade, update, and finally failed to upgrade the original code directly, or a new project, copy the original code to generate the app.

The original can run the app, unexpectedly crashed, and can not intercept the exception. Through debug, found that the crash is related to a new thread, web-level, and found to be able to catch such an exception, the original:Android uncaughtexceptionhandler capture thread crash exception.

My original code is only one sentence:new Downloadapkthread (). Start ();

Modified to:

1Thread thread =NewDownloadapkthread ();2Thread.setuncaughtexceptionhandler (NewThread.uncaughtexceptionhandler () {3 @Override4              Public voiduncaughtexception (thread thread, Throwable ex) {5             //TODO6 System.out.println (Ex.getlocalizedmessage ());7             }8         });9Thread.Start ();

At the breakpoint at line 6, the reason for the thread crash was: "Can ' t create handler inside thread that have not called looper.prepare ()". Continue in the online degree, explained very much, the reason did not look carefully, probably with the master-slave thread of the message distribution related, carry forward the consistent spirit of swallowed, find a solution to solve the simplest solution used: Android Development error can not create handler inside thread T

My method of using toasts, such as toast, appears from the thread code: "Toast." Maketext (mcontext, mcontext.getresources (). getString (r.string. soft_update_fail_downloadapk), Toast. Length_long). Show ();" Code, add Looper in front of it. prepare ();, followed by Looper. loop ();. Of course, also remember import android.os.*;

Resolves two problems, the thread crashes the exception interception, according to the original description, can be in the main activity of the OnCreate method to add the following code, you can intercept all the threads occur when the exception, sounds very good, but I did not try, later there is a need to say it.

Thread.setdefaultuncaughtexceptionhandler (new  Thread.uncaughtexceptionhandler () {public  void  uncaughtexception (thread thread, Throwable ex) {// any one thread exception after unified processing  System.out.println (Ex.getlocalizedmessage ()); Finish ();}});

As for the second question, do not know why the original code is not a problem, now there is a problem, perhaps the Android studio and SDK upgrade caused? No time to delve into it anyway.

Android Development-New thread crashes

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.