Android中的常见FC及解决方式整理

974 阅读1分钟

导致出现Force Close的原因有很多,

  1. 常见的有比如空指针
  2. 类没有找到
  3. 资源没找到
  4. 就连Android API使用的顺序错误也可能导致(比如setContentView()之前进行了findViewById()操作)
  5. 如何避免弹出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); 这句代码都要在那个线程中执行一次