由于未处理的异常导致应用程序崩溃时显示错误页面

时间:2018-11-17 11:09:50

标签: android exception-handling

由于某些未处理的异常,我的应用程序崩溃时,我想启动一个活动。我已经实现了以下方法

+--------------------------+------+------+---------+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| name                     | Type | ABV  | Brewery | Average_Price | Description                                                                                                                                                                                                                                                                                                                       |
+--------------------------+------+------+---------+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Hocus Pocus              | Beer |  4.5 |     812 |             0 | Our take on a classic summer ale.  A toast to weeds, rays, and summer haze.  A light, crisp ale for mowing lawns, hitting lazy fly balls, and communing with nature, Hocus Pocus is offered up as a summer sacrifice to clodless days.Its malty sweetness finishes tart and crisp and is best apprediated with a wedge of orange. |
| Grimbergen Blonde        | Beer |  6.7 |     264 |             0 | None                                                                                                                                                                                                                                                                                                                              |
| Widdershins Barleywine   | Beer |  9.1 |     779 |             0 | None                                                                                                                                                                                                                                                                                                                              |
| Lucifer                  | Beer |  8.5 |     287 |             0 | None                                                                                                                                                                                                                                                                                                                              |
| Bitter                   | Beer |    4 |    1056 |             0 | None                                                                                                                                                                                                                                                                                                                              |
| Winter Warmer            | Beer |  5.2 |    1385 |             0 | None                                                                                                                                                                                                                                                                                                                              |
| Winter Welcome 2007-2008 | Beer |    6 |    1099 |             0 | None                                                                                                                                                                                                                                                                                                                              |
|                          | NULL | NULL |    NULL |          NULL | NULL                                                                                                                                                                                                                                                                                                                              |
+--------------------------+------+------+---------+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
</code>

}

以上是我的应用程序类,我在其中设置了默认的setDefaultUncaughtExceptionHandler,但在有代码开始活动的地方无法启动我的服务,请帮忙。

1 个答案:

答案 0 :(得分:0)

我为未捕获的异常编写了以下课程

公共类UncaughtExceptionHandler实现Thread.UncaughtExceptionHandler {

private Context context;
private Class<? extends AppCompatActivity> erroractivity;

public UncaughtExceptionHandler(Context context, Class<? extends AppCompatActivity> erroractivity) {
    this.context = context;
    this.erroractivity = erroractivity;
}

public static void setUpErrorHandlingActivity(Context context, Class<? extends AppCompatActivity> erroractivity) {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(context, erroractivity));
}


@Override
public void uncaughtException(Thread thread, Throwable throwable) {
    new SystemLog().writeAppRecLog("UNCAUGHT_EXCEPTION", "UnCaught Exception.", (Exception) throwable);
    Intent errorActivityIntent = new Intent(context, erroractivity);
    errorActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    errorActivityIntent.setAction("FROM_ERROR_ACTIVITY");
    context.startActivity(errorActivityIntent);
    Process.killProcess(Process.myPid());
    System.exit(10);
}

}

在我的活动中,我已经注册了此类,因此我可以启动自己的错误活动

        UncaughtExceptionHandler.setUpErrorHandlingActivity(getActivityContext(), ErrorActivity.class);