在Android上使用全局异常处理 [英] Using Global Exception Handling on android
本文介绍了在Android上使用全局异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
使用解决方案访问此页面的人的基本示例:)
public class ChildActivity extends BaseActivity {
@SuppressWarnings(unused)
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int a = 1/0;
}
}
处理错误的类:
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
@Override
public void uncaughtException(Thread paramThread,Throwable paramThrowable){
Log.e(Alert,Lets看看它是否工作!!!);
}
});
}
}
Is there a code example, or a tutorial on how to use the Thread.setDefaultUncaughtExceptionHandler method? Basically I'm trying to display a custom alert dialog, whenever an exception is thrown, in my application. Is it possible to do this? I know it's a little bit tricky to display something on the screen, if the exception is thrown in the UI thread but I don't know any work around for this.
解决方案
Basic Example for someone who comes to this page with a solution :)
public class ChildActivity extends BaseActivity {
@SuppressWarnings("unused")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int a=1/0;
}
}
Class for handling error:
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
Log.e("Alert","Lets See if it Works !!!");
}
});
}
}
这篇关于在Android上使用全局异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文