什么是FC?如何避免FC的发生,另外FC发生时如何捕获相应的uncaught exception?

本文详细介绍了Android应用中ForceClose现象的原因及解决方法,包括常见错误如空指针、类未找到、资源未找到等,并提供了通过实现Thread.UncaughtExceptionHandler接口来避免弹出ForceClose窗口的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在网上看了一些博客但是就是没有答案,所以自己总结了一些答案供大家参考,希望对大家有所帮助,谢谢!

  首先我们要知道什么是FC?它是一个什么东西?全称force close,就是崩溃了,要强制关闭

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

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

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

    importjava.lang.Thread.UncaughtExceptionHandler;

    import android.app.Application;

    public class MyApplication  extends Application implements UncaughtExceptionHandler {

    @Override

    public voidonCreate() {

      // TODOAuto-generated method stub

      super.onCreate();

    }

    @Override

    public void  uncaughtException(Thread thread, Throwable ex) {

      thread.setDefaultUncaughtExceptionHandler(this)

    }

    }

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

Last login: Fri Jun 6 14:03:37 on ttys000 /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea ; exit; liuhuangsheng@liuhuangshengdeMacBook-Air ~ % /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea ; exit; 2025-06-06 14:03:44.965 idea[4826:170191] allVms required 1.8*,1.8+ 2025-06-06 14:03:44.982 idea[4826:170195] Current Directory: /Users/liuhuangsheng 2025-06-06 14:03:44.982 idea[4826:170195] parseVMOptions: IDEA_VM_OPTIONS = /Users/liuhuangsheng/Downloads/jetbra/vmoptions/idea.vmoptions 2025-06-06 14:03:44.983 idea[4826:170195] parseVMOptions: platform=-1 user=-1 file=(null) *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow should only be instantiated on the main thread!' *** First throw call stack: ( 0 CoreFoundation 0x000000019f4aee80 __exceptionPreprocess + 176 1 libobjc.A.dylib 0x000000019ef96cd8 objc_exception_throw + 88 2 CoreFoundation 0x000000019f4d3534 _CFBundleGetValueForInfoKey + 0 3 AppKit 0x00000001a2fc4460 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 260 4 AppKit 0x00000001a3117520 -[NSPanel _initContent:styleMask:backing:defer:contentView:] + 48 5 AppKit 0x00000001a2fc4350 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48 6 AppKit 0x00000001a31174d4 -[NSPanel initWithContentRect:styleMask:backing:defer:] + 48 7 AppKit 0x00000001a2fc30bc -[NSWindowTemplate nibInstantiate] + 216 8 AppKit 0x00000001a2f94af0 -[NSIBObjectData instantiateObject:] + 212 9 AppKit 0x00000001a2f944b4 -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 252 10 AppKit 0x00000001a2f89f6c loadNib + 340 11 AppKit
06-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值