Thursday, December 15, 2016

Tutorial Android Force Close Exception Handler



In software development, we must be faced some of errors. One of the error is force close. There are much causes of force close :
  • The value of attribute is null
  • The activity hasn't defined in AndroidManifest.xml
  • etc
If your application is force close, it will appears a popup unfortunately your app has stopped working.

But this tutorial will handle it. By this tutorial, if your application face an error force close, it won't show a popup nfortunately your app has stopped working. , but the application will open an activity that you chosen.


Maybe the main process like the following :
If Activity B is force close, then go to the MainActivity

PREPARATION


Create 2 java classes :
  1. ForceCloseException.java
  2. ForceCloseDebugge

Now modify ForceCloseException.java, so look likes the following code :


Take a look at the code above, exactly this code :

Intent intent = new Intent(myContext, myContext.getClass());

you can change myContext.getClass() with activity that you wanna open if force close happens. myContext.getClass() is current activity.

Modify ForceCloseDebugger.java, so look likes the following code :


ForceCloseDebugger.java help you to see the causes of the error in your Android Studio's logcat.


USAGE



Put ForceCloseDebugger.handle(this); in onCreate() in your activity

For example how the usage :


That's all.

Related Posts


EmoticonEmoticon