关于Android Force Close 出现的原因 以及解决方法

来源:互联网 发布:怎么成为算法工程师 编辑:程序博客网 时间:2024/05/01 05:26

导致出现Force Close的原因有很多,常见的有比如空指针啦,类没有找到啦,资源没找到,就连Android API使用的顺序错误也可能导致(比如setContentView()之前进行了findViewById()操作)

Force Close有的人说可以用来让应用完全退出 而故意导致这个问题,让程序强制关闭,这种做法我还是不常用。

如何避免弹出Force Close窗口 可以实现Thread.UncaughtExceptionHandler接口的uncaughtException方法 代码如下:

“`
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.Application;
public class MyApplication extends Application implements UncaughtExceptionHandler {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {
thread.setDefaultUncaughtExceptionHandler( this);
}

}
“`再补充一句,想要哪个线程可以处理未捕获异常,thread.setDefaultUncaughtExceptionHandler( this); 这句代码都要在那个线程中执行一次

0 0
原创粉丝点击