3

I have recently incorporated v4 support lib in to my Android application to enable swiping across the tabs. I observe random crashes in the application where the static objects become null.

I gone through the following posts which tell me the story of memory management where Android kills the process to claims the memory, restarts the process and re-launches the latest activity.

I also understood this is the reason why static variables is not the good place to store application data. I am looking into the preferences & DB as an alternatives. But given the complexity of data, this will take sometime.

Edited: I am not storing the data within activities rather having classes with static attributes to preserve a set of configurations at the application level.

My question is, why I face this problem after integrating v4 support lib? Has anyone faced the similar problem with support v4 lib? Does this have anything to do with my problem or its my vague assumption?

Can static variable value be nulled by system in android app?

Android static object lifecycle

static variable null when returning to the app


Here are the logs of my application when it crashes. I observe the NPE in ApplicationConfiguration: getListOrder() method where it refers one of the static vars. Is there any indication from the logs that the system had killed the process and restarted it? Or they indicate something different?

!@Sync 406D/HierarchicalStateMachine( 287): handleMessage: E msg.what=83D/HierarchicalStateMachine( 287): processMsg: ConnectedStateD/WifiStateMachine( 287): ConnectedState{ what=83 when=-4ms arg1=5 }D/HierarchicalStateMachine( 287): handleMessage: XW/PowerManagerService( 287): Timer 0x3->0x3|0x0D/dalvikvm( 2775): GC_EXPLICIT freed 61K, 5% free 6209K/6531K, paused 6ms+2msI/fSharp:BackgroundServiceHandler( 4637): Message received: 3I/fSharp:BackgroundServiceHandler( 4637): list message handler exists, sending messageI/fSharp:BackgroundThread( 4637): Completed processing outbound queueI/fSharp:BackgroundThread( 4637): In do loop...D/AndroidRuntime( 4637): Shutting down VMW/dalvikvm( 4637): threadid=1: thread exiting with uncaught exception (group=0x401b7760)E/AndroidRuntime( 4637): FATAL EXCEPTION: mainE/AndroidRuntime( 4637): java.lang.NullPointerExceptionE/AndroidRuntime( 4637): at com.tfs.fsharpsupport.common.configuration.ApplicationConfiguration.getListOrder(ApplicationConfiguration.java:125)E/AndroidRuntime( 4637): at com.tfs.fsharp.model.ListModel.refreshList(ListModel.java:576)E/AndroidRuntime( 4637): at com.tfs.fsharp.model.ListModel.onListChanged(ListModel.java:332)E/AndroidRuntime( 4637): at com.tfs.fsharp.model.ListModel.handleListMessage(ListModel.java:285)E/AndroidRuntime( 4637): at com.tfs.fsharpsupport.background.BackgroundServiceHandler.handleMessage(BackgroundServiceHandler.java:104)E/AndroidRuntime( 4637): at android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime( 4637): at android.os.Looper.loop(Looper.java:132)E/AndroidRuntime( 4637): at android.app.ActivityThread.main(ActivityThread.java:4028)E/AndroidRuntime( 4637): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime( 4637): at java.lang.reflect.Method.invoke(Method.java:491)E/AndroidRuntime( 4637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)E/AndroidRuntime( 4637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)E/AndroidRuntime( 4637): at dalvik.system.NativeStart.main(Native Method)W/ActivityManager( 287): Force finishing activity com.tfs.fsharp.ui.activity/.StartupActivityE/android.os.Debug( 287): Dumpstate > /data/log/dumpstate_app_errorI/dumpstate( 5407): beginD/HierarchicalStateMachine( 287)

Community
  • 1
  • 1
Mukesh Bhojwani
  • 2,014
  • 4
  • 20
  • 35
  • possibily your apk is now larger and therefore may face memory issues, especially if you have a lot of data in your static vars. however, kill and restart should be indicated by the logs. – njzk2 Mar 12 '13 at 10:03
  • Can you please review the below logs and tell me whether the service got killed ? – Mukesh Bhojwani Mar 12 '13 at 11:47

1 Answers1

1

Obviously your Activities are being killed behind the scene as you swap through tabs. Hope you know the life cycle of the Activity.

As for your information, using static variables delcares inside activities is not a good practice. You'll often encounter Null pointer exceptions. If you insist on using static variables over shared preference or content providers then I think you should follow the method below.

I once had same issue and I had a separate class declared and used it for static variables. With couple of more lines and changes to the Manifest file, you can make them global and use it application wide.

Check out this tutorial on how to do this.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148