First, Reason:
Forceclose, meaning to forcibly close, the current application has a conflict.
Nullpointexection (null pointer), indexoutofboundsexception (subscript out-of-bounds), even the wrong order of the Android API use may be caused (such as Setcontentview () Previous Findviewbyid () operation) and so on a series of uncaught exceptions
Second, how to avoid
How to avoid ejecting the Force close window, you can implement the Uncaughtexception method code for the Thread.uncaughtexceptionhandler interface as follows:
public class Mainactivity extends Activity implements Thread.uncaughtexceptionhandler, View.onclicklistener {PR Ivate list<string> mlist = new arraylist<string> (); Private Button btn; private int pid; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.o Ncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LOG.I ("tag", "--->>oncreate"); Initview (); Sets the handler Thread.setdefaultuncaughtexceptionhandler (this) for handling exceptions; }/** * Initialize control */private void Initview () {btn = (Button) Findviewbyid (R.ID.MAIN_BTN); Btn.setonclicklistener (this); } @Override public void Uncaughtexception (Thread arg0, throwable arg1) {//TODO auto-generated method stub LOG.I ("tag", "Intercept to Forceclose, exception reason:" + "\ n" + arg1.tostring () + "Thread:" +arg0.getid ()); Finish ();//end Current Activity AndroidOid.os.Process.killProcess (Android.os.Process.myPid ()); } @Override public void OnClick (View arg0) {//TODO auto-generated Method Stub switch (Arg0.getid ()) {Case R.id.main_btn:mlist.get (1);//generates abnormal break; Default:break; }} @Override protected void OnPause () {super.onpause (); LOG.I ("tag", "--" onpause "); } @Override protected void OnStop () {//TODO auto-generated Method Stub super.onstop (); LOG.I ("tag", "--->onstop"); } @Override protected void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy (); LOG.I ("tag", "-->ondestroy"); }}
To add, you want which thread can handle the uncaught exception, Thread.setdefaultuncaughtexceptionhandler (this); This code will be executed once in that thread.
In the Uncaughtexception method, the first argument is a thread, and the second argument is an exception.
public void Uncaughtexception (Thread arg0, Throwable arg1) { //TODO auto-generated method stub log.i ("tag", "intercepted to Forceclose, the exception reason is:" + "\ n" + arg1.tostring () + " Thread:" +arg0.getid ()); Finish ();//end Current Activity android.os.Process.killProcess (Android.os.Process.myPid ()); }
Next, look at the log log results:
08-09:27.87410739-10739/example. com.force_anri/tag:--->>onCreate 08-09:31.66410739-10739/example. com.force_anri/tag: intercepted to forceclose,the common cause is : Java.Lang.indexoutofboundsexception:invalid< Span class= "Ace_identifier" >index1, sizeis 0Thread:1
The exception was successfully caught and the activity exited, but it was not safe to exit because the program was unresponsive when you clicked to open the APK again.
In order to solve the above problem, I killed the process in the Uncaughtexception method, the killing process has a lot of methods, here to enumerate a suicide method
Modify the following:
@Override public void Uncaughtexception (Thread arg0, Throwable arg1) { //TODO auto-generated method stub LOG.I ("tag", "intercept to Forceclose, exception reason:" + "\ n" + arg1.tostring ()); Android.os.Process.killProcess (Android.os.Process.myPid ()); Other programs do not change:
3, we can do this not only in the main thread, but also in sub-threads:
It then turns on the child thread during the activity's life cycle, listening for the occurrence of an uncaught exception.
Class Myrunnable extends Thread implements Thread.uncaughtexceptionhandler { @Override public void Run () { TODO auto-generated Method Stub Thread.setdefaultuncaughtexceptionhandler (this); } @Override public void Uncaughtexception (Thread arg0, Throwable arg1) { //TODO auto-generated method stub LOG.I ("tag", "Childthread: Intercepted to Forceclose, exception reason:" + "\ n" + arg1.tostring () + " thread->" +arg0.getid () + " This thread id-> "+thread.currentthread (). GetId () +" "+ Thread.CurrentThread (). GetName ()); Android.os.Process.killProcess (Android.os.Process.myPid ()); } }
Here's the problem: we're obviously caught in a sub-thread of the exception, but how thread id->1 this thread id->1, why threads are the main threaded! Explore this question below.
08-09 19:02:47.734 14483-14483/EXAMPLE.COM.FORCE_ANR i/tag:--->>oncreate08-09 19:02:51.304 14483-14483/ EXAMPLE.COM.FORCE_ANR I/tag:childthread: Intercepted to Forceclose, the reason for the exception is: Java.lang.IndexOutOfBoundsException:Invalid index 1, Size is 0 thread->1 this thread id->1 main
4. Solving the third step problem
We rewrite the sub-thread: Set the exception in the child thread, and don't forget to delete the code that caught the exception in the activity and the code where the exception occurred.
Class Myrunnable extends Thread implements Thread.uncaughtexceptionhandler { int a[]; @Override public Void Run () { //TODO auto-generated method stub Thread.setdefaultuncaughtexceptionhandler (this); int i = a[0];//exception } @Override public void Uncaughtexception (Thread arg0, Throwable arg1) { //TODO auto-g enerated method Stub log.i ("tag", "Childthread: Intercept to Forceclose, exception reason:" + "\ n" + arg1.tostring () + " Thread-> "+arg0.getid () +" This thread id-> "+thread.currentthread (). GetId () +" "+ Thread.CurrentThread (). GetName ( )); Android.os.Process.killProcess (Android.os.Process.myPid ()); } }
In the startup program, see the following log:
08-09 19:08:20.124 16308-16308/example.com.force_anr I/tag:--->> oncreate08-09 19:08:20.124 16308-16341/example.com.force_anr i/tag:childthread: Intercept to Forceclose, the reason for the exception is: Java.lang.NullPointerException:Attempt to read from null array thread->44829 this line Cheng id->44829 thread-4482908-09 19:08:20.254 16349-16349/example.com.force_anr i/tag:--->>onCreate08-09 19:08:20.354 16376-16376/EXAMPLE.COM.FORCE_ANR I/tag:--->>oncreate08-09 19:08:20.354 16376-16411/ EXAMPLE.COM.FORCE_ANR I/tag:childthread: Intercepted to Forceclose, the reason for the exception is: j Ava.lang.NullPointerException:Attempt to read from null array thread->44839 this thread id->44839 Thread-44839
It's like trying to start it two times and see that the thread has changed. So arg0 in this method uncaughtexception (thread arg0, throwable arg1) refers to the thread that has the exception, not necessarily the uncaughtexception The registered thread.
about why Android force Close appears and how to fix it