Android Force Close causes and solutions: androidforce
I. Reasons:
Forceclose indicates that the current application has a conflict.
NullPointExection (NULL pointer), IndexOutOfBoundsException (subscript out-of-bounds), even errors in the sequence of Android API usage may cause (for example, a series of uncaptured exceptions such as findViewById () operations before setContentView ().
Ii. How to avoid
To avoid the Force Close window, you can implement the UncaughtExceptionHandler interface's uncaughtException Method Code as follows:
Public class MainActivity extends Activity implements Thread. uncaughtExceptionHandler, View. onClickListener {private List <String> mList = new ArrayList <String> (); private Button btn; private int pid; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Log. I ("tag", "---> onCreate"); initView (); // sets the handler Thread to handle exceptions. setDefaultUncaughtExceptionHandler (this);}/*** initialization 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", "intercepted forceclose, cause of exception:" + "\ n" + arg1.toString () + "Thread:" + arg0.getId ()); // finish (); // end the current activity android. 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); // 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 ");}}
In addition, Thread. setDefaultUncaughtExceptionHandler (this); the Code must be executed once in that Thread.
In the uncaughtException method, the first parameter is a thread, and the second parameter is an exception.
Public void uncaughtException (Thread arg0, Throwable arg1) {// TODO Auto-generated method stub Log. I ("tag", "intercepted forceclose, cause of exception:" + "\ n" + arg1.toString () + "Thread:" + arg0.getId ()); // finish (); // end the current activity android. OS. process. killProcess (android. OS. process. myPid ());}
Next, let's look at the log results:
08-0918: 50: 27.87410739-10739/example.com. force_anrI/tag: ---> onCreate08-0918: 50: 31.66410739-10739/example.com. force_anrI/tag: intercepted forceclose. Exception cause: java. lang. indexOutOfBoundsException: Invalidindex1, sizeis0Thread: 1
An exception is caught successfully, and the activity exits, but it is not a secure exit because when you click Open apk again, the program does not respond.
To solve the above problem, I killed the process in the uncaughtException method. There are many methods to kill the process. Here I will list a suicide method.
Modify as follows:
@ Override public void uncaughtException (Thread arg0, Throwable arg1) {// TODO Auto-generated method stub Log. I ("tag", "intercepted forceclose, cause:" + "\ n" + arg1.toString (); android. OS. process. killProcess (android. OS. process. myPid (); //} other programs have not changed ..
3. We can do this not only in the main thread, but also in the Child thread:
Start the sub-thread in the activity lifecycle and listen for exceptions that are not captured.
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 forceclose, cause of exception:" + "\ n" + arg1.toString () + "Thread->" + arg0.getId () + "Thread id->" + Thread. currentThread (). getId () + "" + Thread. currentThread (). getName (); android. OS. process. killProcess (android. OS. process. myPid ());}}
Here is a problem: we obviously caught exceptions in the subthread, but how does Thread id-> 1 Thread id-> 1? Why is the Thread the main Thread! This issue is discussed below.
19:02:47-09. 734 14483-14483/example.com. force_anr I/tag: ---> onCreate08-09 19:02:51. 304 14483-14483/example.com. force_anr I/tag: childThread: The forceclose is intercepted. The exception cause is java. lang. indexOutOfBoundsException: Invalid index 1, size is 0 Thread-> 1 Thread id-> 1 main
4. Solve the Problem in step 3
We rewrite the sub-thread: Set exceptions in the sub-thread, and do not forget to delete the code that caught exceptions and the code that encountered exceptions in the activity.
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 forceclose, cause of exception:" + "\ n" + arg1.toString () + "Thread->" + arg0.getId () + "Thread id->" + Thread. currentThread (). getId () + "" + Thread. currentThread (). getName (); android. OS. process. killProcess (android. OS. process. myPid ());}}
View the following log in the startup program:
19:08:20-09. 124 16308-16308/example.com. force_anr I/tag: ---> onCreate08-09 19:08:20. 124 16308-16341/example.com. force_anr I/tag: childThread: The forceclose is intercepted. The exception cause is java. lang. nullPointerException: Attempt to read from null array Thread-> 44829 Thread 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: The forceclose is intercepted. The exception cause is java. lang. nullPointerException: Attempt to read from null array Thread-> 44839 Thread id-> 44839 Thread-44839
It seems that I tried to start it twice and check that the Thread has changed. Therefore, in this method uncaughtException (Thread arg0, Throwable arg1), arg0 refers to the Thread where an exception occurs, not necessarily the Thread registered by uncaughtException.