Android error Threadid=1:thread exiting with uncaught

Source: Internet
Author: User
Tags try catch
<span id="Label3"></p><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">Android error Threadid=1:thread exiting with uncaught exception (group=0x416298c8)</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">Crash when testing in Project development, error as above<br>The error means that there are no caught exceptions in the Thread. Typically catch exceptions using</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">Try {} catch (Exception e) {}</pre></pre> <p><p><br></p></p> <p><p><br></p></p> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">however, in a thread pool, the thread pools catch all exceptions when the task is executed, so that all exceptions in the threads cannot catch the thrown Exception.<br>That is, the try catch catches the Exception.<br>There is an interface in java, Uncaughtexceptionhandler is described as Follows:</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">Static interface Thread.uncaughtexceptionhandler//when Thread terminates abruptly because of an uncaught exception, the interface of the handler is Called.</pre></pre> <p><p><br></p></p> <p><p><br></p></p> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">A method in the thread class</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">static void Setdefaultuncaughtexceptionhandler (thread.uncaughtexceptionhandler Eh)//set When the thread suddenly terminates due to an uncaught exception And there is no default handler that is called when other handlers are defined for the THREAD.</pre></pre> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">We need to implement such an interface uncaughtexceptionhandler, and then set the handler in the main thread of the Program.<br>The code is as follows</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">Import java.lang.Thread.UncaughtExceptionHandler; Implements Uncaughtexceptionhandler public class other extends Activity implements Uncaughtexceptionhandler {// The interface must be implemented uncaughtexception @Override public void uncaughtexception (Thread arg0, throwable arg1) {//handle Exceptions here, Arg1 is a caught exception <c0 />LOG.I ("AAA", "uncaughtexception " + arg1);} }</pre></pre> <p><p><br></p></p> <p><p><br></p></p> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">The exceptions in the thread are not yet captured, and a method needs to be called</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">Thread.setdefaultuncaughtexceptionhandler (this);</pre></pre> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">You can call it in the OnCreate method</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;"><pre class="brush:java; toolbar: true; auto-links: false;">@Override protected void OnCreate (Bundle Savedinstancestate) {//call The following method in the OnCreate method, and then use the thread, The exception Thread.setdefaultuncaughtexceptionhandler (this) can be caught in the uncaughtexception method;}</pre></pre> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">The complete code is as follows</p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"></p></p><pre class="brush:java; toolbar: true; auto-links: false;">Import Package Imports java.lang.Thread.UncaughtExceptionHandler; Implements Uncaughtexceptionhandler public class other extends Activity implements Uncaughtexceptionhandler {@ Override protected void onCreate (Bundle Savedinstancestate) {//TODO auto-generated method stub super.oncreate (savedinst ancestate); Setcontentview (r.layout.th); The following method is called here to catch the exception in the thread Thread.setdefaultuncaughtexceptionhandler (this); Gets the created Button btn1 = (button) Findviewbyid (r.id.btn); Add a click Listener event to BTN1 Btn1.setonclicklistener (new onclicklistener () {@Override public void OnClick (View arg0) {// Call my own method Init (); } }); } public void Init () {//open thread new Handler (looper.getmainlooper ()). post (new Runnable () {@Override public voi D Run () {///here is the try catch is unable to catch the exception of try {/////////////////////////////////////////////method required to be called in thread Purchase.init (context, Iaplistener) Purchase.init (context, iaplistener); } catch (Exception E) {Todo:handle exception log.i ("AAA", "inig" + e); E.printstacktrace (); } } }); }//must Implement Interface uncaughtexception @Override public void uncaughtexception (Thread arg0, throwable arg1) {//handle exception in this case, arg1 is the caught exception LOG.I ("AAA", "uncaughtexception" + arg1); }</pre> <p><p></p></p> <p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;"><p style="font-family:‘microsoft yahei‘;font-size:14px;background-color:#FFFFFF;">Running again will catch the exception in the thread without causing the project to crash directly</p></p><p><p>Android error Threadid=1:thread exiting with uncaught</p></p></span>

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.