我也试试老外写博客= =

怎么防止程序后台运行长时间后切换回来导致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 .

1
2
3
4
5

finish();
Intent intent=newIntent(getApplicationContext(),SingleTaskActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.RESTART_APP,true);
startActivity(intent);

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).

1
2
3
4
5
6
7
8

@Override
protectedvoidonNewIntent(Intent intent){
    super.onNewIntent(intent);
    if(intent.getBooleanExtra(Constants.RESTART_APP,false)){
        finish();
        //TODO start Activity that you defined as MAIN in your manifest.xml
    }
}

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.

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

Android: how to resolve Application’s parameter NullPointerException相关推荐

  1. Android的Proxy/Delegate Application框架

    转自:http://blogs.360.cn/360mobile/2013/11/25/proxydelegate-application/#comment-77 有的时候,为了实现一些特殊需求,如界 ...

  2. Android Studio – Cannot resolve symbol ‘R’

    Android Studio – Cannot resolve symbol 'R' 解决方法: Build -> Clean Project Tools -> Android -> ...

  3. 【Android 应用开发】 Application 使用分析

    博客地址 : http://blog.csdn.net/shulianghan/article/details/40737419 代码下载 : Android 应用 Application 经典用法; ...

  4. android java 静态库_android的android.mk,和application.mk文件编写

    android的android.mk,和application.mk文件编写 1.简介 android.mk是用来描述要编译某个具体的模块,所需要的一些资源,包括要编译的源码.要链接的库等等 appl ...

  5. 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)

    在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值) 参考文章: (1)在Android中自定义捕获Application全局异常,可以 ...

  6. Android Senor Framework (二) Application

    本节主要分析的是java层次相关的软件内容 Application开发 谷歌开发者网站 sensors 中提供了AOSP提供的sensor相关的api介绍及demo: 可以参考下相关资源: 传感器 动 ...

  7. Android开发者指南(4) —— Application Fundamentals

    前言 本章内容为开发者指南(Dev Guide)/Framework Topics/Application Fundamentals,版本为Android2.3 r1,翻译转载并整理自译言:" ...

  8. Android --- Unable to resolve dependency for ‘:app@debug/compileClasspath‘: Could not resolve com.a

    网上说了一堆要修改gradle.properties文件,注释掉后四行,如果没有这个文件呢?如果没有设置代理呢?你可以常识一下方法 buildscript {repositories {maven{ ...

  9. Android Unable to resolve target 'android-8'

    今天导入一个Android的例子程序,出现了Unable to resolve target 'android-8'的错误.刚开始以为是没导入包或者是phoneGap没配置好.可是折腾了好久还是报错. ...

最新文章

  1. echarts.js 做图表的插件
  2. 数据院跨学科交叉人才培养走出国门——中德交换生项目首位社科学子赴德国哥廷根大学交流学习
  3. SimpliVity:我是你的完美选择,HPE!
  4. 参数化之利用CSV Data Set Config从文件读取参数并关联变量
  5. python切片语法-Python新手学习基础之数据类型——字符串的切片截取
  6. 为ListView每个Item上面的按钮添加事件
  7. Leetcode: 4Sum
  8. python3命令行运行程序怎么关闭_python中怎样退出程序运行?
  9. 使用python重命名某个文件下的所有的文件
  10. django restful 请求_Django编写RESTful API(二):请求和响应
  11. python发送json请求_使用Python请求发布JSON
  12. TextView赋值int型,并显示
  13. RedisLockRegistry源码-redis分布式锁
  14. 使用range()以相反的顺序打印列表?
  15. 关于VFS文件系统中的superblock、inode、d_entry和file数据结构
  16. MacOS11.6.7上安装Axure9.003720无法预览问题
  17. 强大的Win7计算器
  18. QGIS Server安装教程
  19. 教你炒股票28:回复(一)
  20. [转载]工作5年的回顾,即成长历程

热门文章

  1. css z-index层重叠顺序
  2. 算法_bitmap算法
  3. HashMap深度解析(二)
  4. 使用 CodeIgniter 框架快速开发 PHP 应用(二)
  5. Android文本框EditText显示为多行
  6. CLRS2e读书笔记—Chapter5 Appendix C
  7. 2012 MUTC 7 总结
  8. 关于python中文处理
  9. VS2008常用快捷键
  10. pythonapriori算法特点_Python --深入浅出Apriori关联分析算法(一)