Catching run-time exceptions
When the app is running, it is inevitable that the runtime exception will occur, which causes the program to crash. Of course, if you are developing in debug mode, you can view the exception message by looking at the Logcat log for processing. However, if we release the version and the user crash out when they use it, they will not be able to view the exception information, and it will be difficult to find a bug to solve the problem.
Fortunately, in the Java threading class, there is a workaround for the above problem: catching unhandled exceptions in the thread. Because crash, throws the exception is because does not have in the app catch processing, will throw to the system, if we at this time to this can treat this exception to handle, it is best, so can print the exception information, can publish to the server, for the developer to view.
One, write a Crashhanler class
PackageCom.raise.wind.utils;ImportAndroid.os.Environment;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.IOException;/** * Created by Yu on 2015/7/15. * * Public class Crashhandler implements Thread. Uncaughtexceptionhandler { Public StaticCrashhandler instance;Private Crashhandler() { } Public StaticCrashhandlerget_instance() {if(Instance = =NULL)NewCrashhandler ();returnInstance } Public void Init() {Thread.setdefaultuncaughtexceptionhandler ( This); }@Override Public void uncaughtexception(thread thread, throwable ex) {SaveFile (Ex.getmessage (),"Crash.txt");//Exit program //Here because it is our own handling of the exception, you must manually quit the program, or the system out of a crash waiting stateAndroid.os.Process.killProcess (Android.os.Process.myPid ()); System.exit (1); } Public Static void SaveFile(String data, string file_name) {File Sdpath =NewFile (Environment.getexternalstoragedirectory (). GetAbsolutePath () + File.separator +"Aacrash"+ File.separator +"Cache");if(!sdpath.exists ()) {Sdpath.mkdirs (); } File File =NewFile (Sdpath, file_name); FileOutputStream fos =NULL;Try{fos =NewFileOutputStream (file); Fos.write (Data.getbytes ("UTF-8")); }Catch(Exception e) {E.printstacktrace (); }finally{if(Fos! =NULL)Try{Fos.close (); }Catch(IOException e) {E.printstacktrace (); } } }}
Two, in the application statement
Remember to configure the Androidanifest.xml in the
package com.raise.wind.app;import android.app.Application;import com.raise.wind.utils.CrashHandler;/** * Created by yu on 2015/7/15. */publicclass APP extends Application { @Override publicvoidonCreate() { super.onCreate(); CrashHandler.get_instance().init(); }}
Three, test
This declares that the catch exception is given to the Crashhandler class to handle in our thread.
Now let's write a null pointer exception to test:
package com.raise.wind.crashproject;import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import Android.widget.TextView; public class mainactivity Span class= "Hljs-keyword" >extends actionbaractivity { TextView TextView; @Override protected void oncreate (Bundle savedinstancestate) {super . OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Textview.settext ( "text" ); }}
Open the app, discover the program immediately crash, open File management can find the file saved in the program, there is an exception message
to start activity ComponentInfo{com.raise.wind.crashproject/com.raise.wind.crashproject.MainActivity}tovirtualmethod ‘void android.widget.TextView.setText(java.lang.CharSequence)‘ on a null object reference
Of course, this is just the tip of the beginning of the exception message, if you want to print it all out, use the following code:
Could notExecuteMethod of the ActivityAndroid.View.View$.OnClick(view.java:4010)Android.View.View.PerformClick(view.java:4759)Android.View.View$PerformClick.Run(view.java:19770)Android.OS.Handler.Handlecallback(handler.java:739)Android.OS.Handler.DispatchMessage(handler.java:95)Android.OS.Looper.Loop(looper.java:135)Android.app.Activitythread.Main(activitythread.java:5235)Java.Lang.reflect.Method.Invoke(Native Method)Java.Lang.reflect.Method.Invoke(Method. java:372)com.Android.Internal.OS.Zygoteinit$Methodandargscaller.Run(zygoteinit.java:906)com.Android.Internal.OS.Zygoteinit.Main(zygoteinit.java:701)caused by NULLJava.Lang.reflect.Method.Invoke(Native Method)Java.Lang.reflect.Method.Invoke(Method. java:372)Android.View.View$.OnClick(view.java:4005)Android.View.View.PerformClick(view.java:4759)Android.View.View$PerformClick.Run(view.java:19770)Android.OS.Handler.Handlecallback(handler.java:739)Android.OS.Handler.DispatchMessage(handler.java:95)Android.OS.Looper.Loop(looper.java:135)Android.app.Activitythread.Main(activitythread.java:5235)Java.Lang.reflect.Method.Invoke(Native Method)Java.Lang.reflect.Method.Invoke(Method. java:372)com.Android.Internal.OS.Zygoteinit$Methodandargscaller.Run(zygoteinit.java:906)com.Android.Internal.OS.Zygoteinit.Main(zygoteinit.java:701)caused by attempt to Invoke Virtual Method‘void Android.Widgets.TextView.SetText(java.lang.CharSequence)‘ on a NULL Object Referencecom.Raise. Wind.Crashproject.mainactivity.Click_1(mainactivity.java:21)Java.Lang.reflect.Method.Invoke(Native Method)Java.Lang.reflect.Method.Invoke(Method. java:372)Android.View.View$.OnClick(view.java:4005)Android.View.View.PerformClick(view.java:4759)Android.View.View$PerformClick.Run(view.java:19770)Android.OS.Handler.Handlecallback(handler.java:739)Android.OS.Handler.DispatchMessage(handler.java:95)Android.OS.Looper.Loop(looper.java:135)Android.app.Activitythread.Main(activitythread.java:5235)Java.Lang.reflect.Method.Invoke(Native Method)Java.Lang.reflect.Method.Invoke(Method. java:372)com.Android.Internal.OS.Zygoteinit$Methodandargscaller.Run(zygoteinit.java:906)com.Android.Internal.OS.Zygoteinit.Main(zygoteinit.java:701)
This class typically combines log4j to log logs, and when crash occurs, the log file is sent to the server.
This allows the program to view the crash messages on the user's mobile phone, allowing us to handle exceptions that were not found during debug mode development.
Click I download source code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android_app-level exception handling