Analysis of the abnormal cause of Android Force Close and its solution _android

Source: Internet
Author: User
Tags log log stub throwable

First, the reason:

Forceclose, meaning forced shutdown, the current application has a conflict.

Nullpointexection (null pointer), indexoutofboundsexception (subscript out of bounds), even the sequence errors used by the Android API may result (e.g. Setcontentview () A Findviewbyid () operation preceded by a series of not-caught exceptions

Second, how to avoid

How to avoid popping 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 {private Li
st<string> mlist = new arraylist<string> ();
Private Button btn;
private int pid; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (savedins
Tancestate);
Setcontentview (R.layout.activity_main);
LOG.I ("tag", "--->>oncreate");
Initview ();
Sets the handler Thread.setdefaultuncaughtexceptionhandler (this) to handle the exception; /** * Initialize control/private void Initview () {btn = (Button) Findviewbyid (R.ID.MAIN_BTN); Btn.setonclicklistener (this); @Ov Erride public void Uncaughtexception (Thread arg0, throwable arg1) {//TODO auto-generated Method stub log.i ("tag", "Intercept fo
Rceclose, the reason for the exception is: "+" \ n "+ arg1.tostring () +" Thread: "+arg0.getid ());
Finish ()//End Current Activity android.os.Process.killProcess (Android.os.Process.myPid ()); @Override public void OnClick (View arg0) {//TODO auto-generated the method stub switch (arg0.getid()) {case R.id.main_btn:mlist.get (1);//generating exception 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");  }
}

Add another sentence, Thread.setdefaultuncaughtexceptionhandler (this) to which thread can handle the catch exception; This code is 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", "Intercept Forceclose, the reason for the exception 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-0918:50:27.87410739-10739/example.com.force_anri/tag:--->>oncreate
08-0918:50:31.66410739-10739/example.com.force_anri/tag: Intercepted to Forceclose, the reason for the exception is:

Java.lang.indexoutofboundsexception:invalidindex1,sizeis0thread:1

The exception was caught successfully, and the activity also exited, but it was not a safe exit because when you clicked Open apk again, the Discovery program was unresponsive.

In order to solve the above problem, I kill the process in the Uncaughtexception method, kill the process there are a lot of ways to enumerate a suicide method

Modified as follows:

@Override public 
void Uncaughtexception (Thread arg0, Throwable arg1) { 
//TODO auto-generated method stub 
LOG.I ("tag", "intercepted to Forceclose, the reason for the exception is:" + "\" + 
arg1.tostring ()); 
Android.os.Process.killProcess (Android.os.Process.myPid ()); //
}

Other programs have not changed.

3, not only can we do this in the main thread, but we can also do it in a child thread:

The child thread is then opened in the life cycle of the activity, listening for the occurrence of the exception that was not caught.

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 is a problem: we are obviously caught in the child thread exception, but how to thread the id->1 this thread id->1, why the thread is the main thread! Explore the issue below.

08-09 19:02:47.734 14483-14483/EXAMPLE.COM.FORCE_ANR i/tag:--->>oncreate
08-09 19:02:51.304 14483-14483/ EXAMPLE.COM.FORCE_ANR I/tag:childthread: Intercepted to Forceclose, the reason for the exception is:

4. Solving the third step problem

We rewrite the child thread: Set the exception in the child thread, and do not forget to delete the code that caught the exception in the activity and the code that has the exception.

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- Generated method stub
log.i ("tag", "Childthread: Intercepted to Forceclose, the reason for the exception is:" + \ 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:--->>oncreate
08-09 19:08:20.124 16308-16341/ EXAMPLE.COM.FORCE_ANR I/tag:childthread: Intercepted to Forceclose, the reason for the exception:
java.lang.NullPointerException:Attempt to read From null array thread->44829 this thread id->44829 Thread-44829
08-09 19:08:20.254 16349-16349/example.com.force_ ANR I/tag:--->>oncreate
08-09 19:08:20.354 16376-16376/example.com.force_anr i/tag:--->>onCreate
08-09 19:08:20.354 16376-16411/example.com.force_anr i/tag:childthread: Intercepted to Forceclose, the reason for the exception is:

It's like trying to start two times to see if the thread has changed. So arg0 in this method uncaughtexception (thread arg0, throwable arg1) refers to the thread where the exception occurred, not necessarily the thread of uncaughtexception registration.

The above is a small set to introduce the Android Force close appearance of the abnormal cause analysis and solutions, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.