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

导致出现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); 这句代码都要在那个线程中执行一次

posted @ 2011-10-27 15:31  jy02432443  阅读(29164)  评论(0编辑  收藏  举报