我正在尝试在我的Android应用程序中实现全局异常处理,但我无法处理NullPointerException。
在Application子类的onCreate方法中:
@Override
public void onCreate()
{
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread arg0, Throwable arg1)
{
Log.v("UncaughtException", arg1.getCause().getMessage());
}
});
}
如果我尝试的话,在我的主要活动的onCreate方法中:
int n = 10 / 0; //The log show me divide by zero exception
int[] vet = new int[] {1,2,3};
int _n = vet[5]; // The log show me index out of bounds exception
But if I try:
String s = null;
s.substring(0, 10); //The Application crash but nothing is logged
我做错了什么?