Android : Android: member variable becoming null

on Monday, September 8, 2014


I have an activity - MainActivity, where I initialize member variable in onCreate(...) method. This variable is then used in onStart() and onStop() methods.


Pretty simple, huh? Now the issue is, that from my users, I started to get NullPointerException crash reports, which happens in onStop() method.


I've been extensively searching for the cause, and I understand that static variables may become null when android decides to free up memory, however I couldn't find a case with member variable, which is initialized in onCreate(). The code is following:



public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, SharedPreferences.OnSharedPreferenceChangeListener {
private MySvc mySvc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySvc = DI.i().getMySvc();
...
}

@Override
protected void onStart() {
super.onStart();
//here I get NPE
mySvc.start();
}

...

@Override
protected void onStop() {
super.onStop();
mySvc.start();
}
}


}


To make things even more mysterious, I started to get this crash reports only from certain versions of app, however the old versions have same behavior in onStart() and onStop().


I will be grateful for any hint.


0 comments:

Post a Comment