I've got an activity, where I initialize some static variable (Menu of the app). In another activity I use these variable in some if-clauses.
If I am in the 2nd activity and press "Home" button to get the app to the background and resume it after a few minutes or immediately the app will still work. If I wait for about 10 minutes before resumeing it crashs.
The smartphone I was using has got many widgets and apps, which will refresh automatically. Because of that I think the variable got deleted from the memory, because it gets full. If I resume the app will crash at a point, where I am just using an if-clause like:
if(couldBeTrue){
//do something
}
I can't conceive to another idea which could cause this problem.
Do you think my assumption is correct? What is the best way to debug it?
Edit: I am sorry for the poor information. I give it another try.
I've got 3 Class:
OpenFeintApp extends Application: starts everytime I start or resume the App (only contains variable and some code for OpenFeint)
MenuActivity extends Activity: This activity is the launcher activity. Contains the needed static variable. Those variable are for settings. And there are some final static variable, which I change to be able to export different versions easily.
public final static boolean EXTRA_CONTENT_ONE = false;
public final static boolean EXTRA_CONTENT_TWO = true;
public final static boole开发者_如何学Pythonan EXTRA_CONTENT_THREE = false;
public static int SETTINGS_ONE = 3;
public static int SETTINGS_TWO = 0;
public static int SETTINGS_THREE = 1;
public static int SETTINGS_FOR = 5;
(will not be called if I resume the app after I pause the app while playing in GameActivity)
GameActivity extends Activity (AdMob integrated): Won't crash after resume if I resume after a few minutes. (Galaxy S, which refresh pretty much things automatically) Will crash if I pause this activity and resume it after about 10 minutes. Before this activity resumes OpenFeintApp will be called.
I got a NullPointerException at this point:
if (MenuActivity.EXTRA_CONTENT_ONE) //This variable is false
Instead of using the public static
of a member variable in your Activity
you should consider the use of global variables. In this way your variables will remain as long as your application is alive.
To rephrase your assumption: I assume it's not safe to use the static data of another activity with the expectation that it will be there any time my other activity exists. That's definitely a valid assumption.
Your activities include onPause() and onResume() methods which you can override and you can store and retrieve state data. It would be minimally better to make use of them.
The right solution though includes one that does not require accessing static data members at all.
精彩评论