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

2013/03/21 23:36
阅读数 341
关于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); 这句代码都要在那个线程中执行一次
展开阅读全文
加载中
点击加入讨论🔥(1) 发布并加入讨论🔥
1 评论
6 收藏
1
分享
返回顶部
顶部