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 :


package com.putuguna.forceclosehandling;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* Created by putuguna on 26/10/16.
*/
public class ForceCloseException implements java.lang.Thread.UncaughtExceptionHandler {
private final Activity myContext;
public ForceCloseException(Activity context) {
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
System.out.println("*****CAUSE OF ERROR*****\n\n");
System.out.println("FORCE CLOSE CAUSED : " + stackTrace.toString());
System.out.println("**** DEVICE INFORMATION ***\n\n" +
"Brand : " + Build.BRAND + "\n" +
"Device : " + Build.DEVICE + "\n" +
"Model : " + Build.MODEL + "\n" +
"ID : " + Build.ID + "\n" +
"Product : " + Build.PRODUCT + "\n" +
"SDK : " + Build.VERSION.SDK + "\n" +
"Release : " + Build.VERSION.RELEASE + "\n" +
"Incremental : " + Build.VERSION.INCREMENTAL + "/n");
Intent intent = new Intent(myContext, myContext.getClass());
intent.putExtra("bugs", stackTrace.toString());
myContext.startActivity(intent);
myContext.finish();
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
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 :

package com.putuguna.forceclosehandling;
import android.app.Activity;
/**
* Created by putuguna on 26/10/16.
*/
public class ForceCloseDebugger {
public static void handle(Activity context){
if (!BuildConfig.BUILD_TYPE.equalsIgnoreCase("debug")) {
Thread.setDefaultUncaughtExceptionHandler(new ForceCloseException(context));
String errorCaused = context.getIntent().getStringExtra("bugs");
System.out.println("FORCE CLOSE CAUSED BY : " + errorCaused);
}else{
Thread.setDefaultUncaughtExceptionHandler(new ForceCloseException(context));
String errorCaused = context.getIntent().getStringExtra("bugs");
System.out.println("FORCE CLOSE CAUSED BY : " + errorCaused);
}
}
}

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 :

package com.putuguna.forceclosehandling;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by putuguna on 26/10/16.
*/
public class SecondActivity extends AppCompatActivity {
private Button btnForceCLose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_force_close);
ForceCloseDebugger.handle(this);
}
}
view raw Usage.java hosted with ❤ by GitHub

That's all.

Related Posts


EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)