机器人 - 如何让内所发生
问题描述:
嘿家伙,我要处理我的Android应用程序内所有异常在一个中心位置的异常的类和方法名,它是应用程序类机器人 - 如何让内所发生
我有一个方法,像下面在我的应用程序类:
public void HandleException(Exception e)
{
String appName = getString(R.string.app_name);
String className = ""; // magic method to find out the original class name where the exception occurred
String methodName = ""; // magic method to find out the original method name where the exception occurred
Log.e(appName, className + " : " + methodName + " - " + e.getMessage());
}
一切,现在缺少的是神奇的方法,填充我的className和methodName的变量与原来的类和方法内发生异常
谁能帮助
在此先感谢...
答
使用e.getStackTrace()
,通过这个阵列(使用StackTraceElement.getClassName()
)搜索类名与包名 - 这里是你的类名“魔术方法”。然后获得方法名称StackTraceElement.getMethodName()
。
当你写的 - 在与您的包名类名 - 如何将我知道的类名来搜索作为异常可以在任何类中发生......在异常 – Raj 2011-04-01 13:43:08
类名称应包含你的包名,所以你需要例如,以'com.raj.application'搜索。 'if(element.getClassName()。contains(yourPackageName)){//它是你的类}' – ernazm 2011-04-01 13:56:45