博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android: how to resolve Application’s parameter NullPointerException
阅读量:6840 次
发布时间:2019-06-26

本文共 3123 字,大约阅读时间需要 10 分钟。

我也试试老外写博客= =

怎么防止程序后台运行长时间后切换回来导致application里的value被清空而crash。

 

1. Scenario

You have a customize Application (named MyApplication). and you initialize some parameters in MyApplication. When you press 'Home' , your app will be suspended by system . If your memory is not enough, the system will kill your app (include MyApplication). In this case ,  When you switch back to your app . if you access the parameter in MyApplication , the app crashed .

2. How to reproduce this bug manually.

You may not realize this before until the crash log from Umeng or HockeyApp , but you don't know when does this happen , only thing you can do is that wish user don't suspend your app or exit app when they want to brower other apps .

but you can reproduce it .

2.a) run your app through Eclipse , go to the Activity which may access the parameter in MyApplication.

2.b) click 'Home' , suspend your app.

2.c) go to DDMS , choose your process (com.stay.test) ,and stop it .

2.d) now , long press 'Home' or press 'Menu' to resume your app .

2.e) app crash

3.How to resolve

3.a) if you have a good programming style. and you have customized Activity as a super class . like BaseActivity , BaseFragmentActivity. And also you know Activity's lifecircle very clear , you may have a SingleTask Activity always on the bottom of activity stack. In this case , this bug can easily be fixed , just like how to exit app in any Activity

3.b) Well , if your code is not like that , pls change your frame , if not , you will find it hard to resovle bugs when the app became bigger.

3.c) set a flag in MyApplication , to represent your app's state , (-1 default , 0 logout ,1 login)

3.d) in BaseActivity or BaseFragment , override onCreate(Bundle savedInstanceState).

check the flag in MyApplication ,

if is -1 , you should exit app and enter app just like user first to use your app . and the parameters in MyApplication would be initialized in correct workflow.

how to exit? simple .

this will jump to the Activity which is SingleTask .  and with flag 'clear top'. all the activities exclude 'single task' in stack will be removed , and the 'single task' would be brought to top of stack . in this case , if you call finish() in 'single task' activity , you will exit app. cause the 'single task' is already exist , so it would not call onCreate(Bundle savedInstanceState), it will call onNewIntent(Intent intent).

4.Check if this bug is fixed , you should test all activities you have , and make sure it is fixed . it may have different case , and you need to give a special solution .

 

p.s.

解决方案已经写的很明白,概不提供额外解释与说明,如果不能解决,请支付酬劳让我提供服务。(源码和讲解)

转载地址:http://sowul.baihongyu.com/

你可能感兴趣的文章
照片切换
查看>>
求矩阵的局部极大值(15)
查看>>
Eclipse详细设置护眼背景色和字体颜色并导出
查看>>
Android Gradle 构建工具(Android Gradle Build Tools)是什么?
查看>>
AOP和IOC的作用
查看>>
Apache的Rewrite规则详细介绍
查看>>
ruby环境sass编译中文出现Syntax error: Invalid GBK character错误解决方法
查看>>
go语言的selector
查看>>
ipa 打包遇到的坑
查看>>
正则表达式
查看>>
js之操作JSON数据
查看>>
Design Hit Counter
查看>>
BZOJ 3576 江南乐
查看>>
NAT网络地址转换
查看>>
DateTime格式化问题
查看>>
杭电2097--Sky数
查看>>
杭电1754--I Hate It(线段树)
查看>>
AS莫名报错 Error:Could not download junit.jar (junit:junit:4.12): No cached version available
查看>>
Kendo UI 简单使用
查看>>
FCKeditor的使用说明
查看>>