【问题标题】:Global error handling in Android application with Roboguice使用 Roboguice 在 Android 应用程序中进行全局错误处理
【发布时间】:2012-01-18 13:09:36
【问题描述】:

我正在开发一个使用 Roboguice 依赖注入框架的 Android 应用程序。 所以大多数时候我们扩展 RoboActivity、RoboListActivity 和类似的。

现在我想介绍一些全局错误处理,它会在应用程序崩溃时显示一些警报或错误活动。

我以前通过实现这样的基本活动来做到这一点:

public class BaseActivity extends Activity
{


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Thread.setDefaultUncaughtExceptionHandler(new GeneralError(this));
}

我在其中定义了默认异常处理程序和所有其他活动,然后从该处理程序派生。

现在我想知道 Roboguice 是如何实现的?

【问题讨论】:

    标签: android error-handling roboguice


    【解决方案1】:

    这里有一些粗略的伪代码可以帮助您入门。它使用roboguice events 使这些横切关注点变得更容易一些。

    public class GlobalErrorHandler {
      // injects the current activity here
      @Inject Context context;
    
    
      public void onCreate(@Observes OnCreateEvent e) {
        // Wires up the error handling
        Thread.setDefaultUncaughtExceptionHandler(new GeneralError(context));
      }
    }
    
    public class MySpecificActivity {
      // required in every activity that needs error handling
      @Inject GlobalErrorHandler errorHandler;
    
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-14
      • 2013-03-06
      • 2019-11-08
      • 2013-12-17
      • 1970-01-01
      • 2012-08-11
      • 2019-07-26
      • 2023-04-04
      相关资源
      最近更新 更多