您的位置:首页 > 移动开发 > Android开发

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

2016-06-23 14:12 483 查看
在安卓页面开发中,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的时候在判断页面是否已回收就行了。

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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: