Android: how to resolve Application’s parameter NullPointerException

來源:互聯網
上載者:User

我也試試老外寫部落格= =

怎麼防止程式後台運行長時間後切換回來導致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 .

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

 
12345678 @OverrideprotectedvoidonNewIntent(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.

解決方案已經寫的很明白,概不提供額外解釋與說明,如果不能解決,請支付酬勞讓我提供服務。(源碼和講解)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.