Android 页面回收引起的闪退问题的解决方法

本文深入探讨了Android开发中Fragment导致的常见闪退问题,解析了闪退的根本原因,并提出了有效的解决方案,强调了正确处理网络请求的重要性。

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


在安卓页面开发中,fragement的灵活使用极大方便了程序员的工作,并且由Fragment 替代TabActivity来做导航,性能也更好,所以fragement被程序员广泛使用中。但是fragement相对activity,也更容易被系统所回收。如果当前的fragement从事了一些异常操作或者注册了广播事件,当事件完成操作来送达通知,但是fragement又已经被系统回收了,这时系统就会重新初始化/加载该fragement,如果原先的数据状态未保存或者异常未处理,就很容易引起空指针异常而闪退。


如下:

java.lang.NullPointerException
	at android.view.LayoutInflater.from(LayoutInflater.java:216)
	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionHallFragment.c(SourceFile:491)
	at com.lebo.xxx.bbbb.widget.SlidingTabStrip.a(SourceFile:191)
	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionHallFragment.d(SourceFile:234)
	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionHallFragment.a(SourceFile:341)
	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionHallFragment.a(SourceFile:54)
	at zs.a(SourceFile:300)
	at zs.onSuccess(SourceFile:277)
	at com.lebo.xxx.bbbb.framework.http.HttpHandler.onProgressUpdate(SourceFile:152)
	at com.lebo.xxx.bbbb.framework.core.AsyncTask$InternalHandler.handleMessage(SourceFile:503)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5314)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
	at dalvik.system.NativeStart.main(Native Method)
展开                                     

没有经验的工程师处理该问题时,想当然的就做 非空 的限制判断。这个闪退问题貌似解决了,但是他就像洪水一下,从另外一个地方泄洪了,如下:

java.lang.NullPointerException

	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionListFragment.a(SourceFile:268)
	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionHallFragment.a(SourceFile:350)
	at com.lebo.xxx.bbbb.ui.auction.auctionhall.AuctionHallFragment.a(SourceFile:55)
	at aah.a(SourceFile:302)
	at aah.onSuccess(SourceFile:279)
	at com.lebo.xxx.bbbb.framework.http.HttpHandler.onProgressUpdate(SourceFile:152)
	at com.lebo.xxx.bbbb.framework.core.AsyncTask$InternalHandler.handleMessage(SourceFile


于是再想当然的加非空判断

java.lang.IllegalStateException: Activity has been destroyed

	at z.a(SourceFile:1460)
	at l.a(SourceFile:634)
	at l.b(SourceFile:617)
	at com.lebo.mychebao.netauction.ui.auction.auctionhall.AuctionHallFragment.T(SourceFile:406)
	at com.lebo.mychebao.netauction.ui.auction.auctionhall.AuctionHallFragment.d(SourceFile:248)
	at com.lebo.mychebao.netauction.ui.auction.auctionhall.AuctionHallFragment.a(SourceFile:363)
	at com.lebo.mychebao.netauction.ui.auction.auctionhall.AuctionHallFragment.a(SourceFile:55)
	at aap.a(SourceFile:322)
	at aap.onSuccess(SourceFile:299)
	at com.lebo.mychebao.netauction.framework.http.HttpHandler.onProgressUpdate(SourceFile:152

这下,程序员傻眼了,没辙了。 其实,这个闪退的根本原因是收到网络请求返回的时候,页面已经被回收掉了,如果继续处理该网络请求的话,就会存在各种各样的未知问题。 所以只要在页面OnDestroy的时候取消掉网络请求即可。当然如果还不放心或者由于网络框架支持的还不是很到位,那就在onSuccess的时候在判断页面是否已回收就行了。


其实,闪退问题就象人生病了一样,不能头疼医头,脚痛医脚,要找到根本原因,对症下药才能治愈。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值