android-Small set up a permanent resolver problem that terminates abnormally because of an uncaught exception

Source: Internet
Author: User
Tags terminates

(i) Preface
Dear Lunch shoes, are not often because of their own programs in the non-layer caught exception caused the program to terminate unexpectedly painful? Well, yes. However, we do not fear, today to share a dongdong can solve everyone this kind of distress, roar!

(ii) Uncaughtexceptionhandler interface
This interface, as the name implies, is the exception that is not handled in the handler, and it is the exception that caused the program to terminate when the system throws an abnormal! So, how do you use it in Android?

(c) How to use Uncaughtexceptionhandler
1. First, we have to create a specific class of uncaughtexceptionhandler, such as:

 Public classCrashhandlerImplementsUncaughtexceptionhandler {Private StaticCrashhandler instance;//Singleton Reference, here we make a singleton, because we only need a Uncaughtexceptionhandler instance in an application         PrivateCrashhandler () {} Public synchronized StaticCrashhandler getinstance () {//synchronization method to avoid exceptions in single-threaded environments         if(Instance = =NULL) {instance=NewCrashhandler (); }         returninstance; }          Public voidInit (Context ctx) {//Initialize, set the current object to the Uncaughtexceptionhandler processorThread.setdefaultuncaughtexceptionhandler ( This); } @Override Public voidUncaughtexception (thread thread, Throwable ex) {//when an unhandled exception occurs, you will come here. LOG.D ("Sandy", "uncaughtexception, Thread:" +Thread+ "Name:" + thread.getname () + "ID:" + thread.getid () + "Exception:" +ex); String ThreadName=Thread.getname (); if("Sub1". Equals (ThreadName)) {LOG.D ("Sandy", "" "xxx); }Else if(){                       //Here we can discriminate according to the thread name, and we can also write the exception information to the file for later analysis.                }     } }

2. Next, we customize the application class

 Public class extends Application {     @Override     publicvoid  onCreate () {         super . OnCreate ();          = crashhandler.getinstance ();          // set our exception processor to Uncaughtexceptionhandler processor in Appliction      } }

3. Configuring the Androidmanifest.xml File
Since we are using custom application, we want to declare it in the Androidmanifest.xml file

< Application          Android:icon = "@drawable/ic_launcher"          android:label= "@string/app_name"         android:name= ". Ourapplication "         android:debuggable=" true "         >

4. Testing
We start a thread in the activity and then throw an exception inside the thread to see what the program will do.

Button BTN =(Button) Findviewbyid (R.ID.BT); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {thread thread=NewThread (NewRunnable () {@Override Public voidrun () {LOG.D ("Sandy", "I am a sub thread"); String s=NULL;  S.tostring (); //Throw Nullpointexception                     }                 }, "Sub Thread"); Thread.Start (); }

5. Results
Because we have handlers for the default unhandled exception, the following log information is printed without throwing an exception causing the program to terminate abnormally
D/sandy (2228): I am a Sub thread
D/sandy (2228): Uncaughtexception, thread:thread[sub Thread,5,main] name:sub thread Id:148exception:java.lang.nul Lpointerexception

What are we waiting for? Quickly add the default unhandled exception handler in your application! There is no longer a program crash because of an uncaught exception: ^_^

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.