Android: how to resolve application's parameter nullpointerexception

Source: Internet
Author: User

I also try writing a blog =

How to PreventProgramAfter a long time of running in the background, switching back causes the value in the application to be cleared and 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 susponded 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 shocould exit app and enter app just like user first to use your app. And the parameters in myapplication wocould be initialized in correct workflow.

How to exit? Simple.

12345 Finish ( ) ; Intent Intent = New Intent ( 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' wocould 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 wocould not call oncreate (bundle savedinstancestate), it will call onnewintent (intent ).

12345678 @ Override Protected Void Onnewintent ( 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 shoshould 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.

The solution has been clearly written and does not provide additional explanations and instructions. If you cannot solve the problem, please pay for it and let me provide the service. (Source code and explanation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.