Avoid force close error – Android java error handling

Activity Forums Android mobile app Avoid force close error – Android java error handling

  • This topic is empty.
  • Creator
    Topic
  • #125
    adcocitclass
    Keymaster

    To avoid force close error in Android app, use below java code as sample, it will also log errors and customize according to your requirement.


    package com.indiacitys.forceclose;

    import java.io.PrintWriter;
    import java.io.StringWriter;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Build;

    public class ExceptionHandler implements
    java.lang.Thread.UncaughtExceptionHandler {
    private final Activity myContext;
    private final String LINE_SEPARATOR = "n";

    public ExceptionHandler(Activity context) {
    myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {
    StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    StringBuilder errorReport = new StringBuilder();
    errorReport.append("************ CAUSE OF ERROR ************nn");
    errorReport.append(stackTrace.toString());

    errorReport.append("n************ DEVICE INFORMATION ***********n");
    errorReport.append("Brand: ");
    errorReport.append(Build.BRAND);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("Device: ");
    errorReport.append(Build.DEVICE);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("Model: ");
    errorReport.append(Build.MODEL);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("Id: ");
    errorReport.append(Build.ID);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("Product: ");
    errorReport.append(Build.PRODUCT);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("n************ FIRMWARE ************n");
    errorReport.append("SDK: ");
    errorReport.append(Build.VERSION.SDK);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("Release: ");
    errorReport.append(Build.VERSION.RELEASE);
    errorReport.append(LINE_SEPARATOR);
    errorReport.append("Incremental: ");
    errorReport.append(Build.VERSION.INCREMENTAL);
    errorReport.append(LINE_SEPARATOR);

    Intent intent = new Intent(myContext, AnotherActivity.class);
    intent.putExtra("error", errorReport.toString());
    myContext.startActivity(intent);

    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(10);
    }

    }

  • You must be logged in to reply to this topic.