I followed the advice given here in several posts on how to declare global contants:
public class Constants {
public static final int i1 = 1;
public static final int i2 = 2;
}
I just include this class in my project and refer to the constants like this:
in any other class...
GlobalsVars.gi1 = Constants.i1;
(ps I hope this is OK and do not need to do anything to the Constants class like initializing or anything.)
But as I found out here: assigning int to Integer using static global variables is not a good idea. My app crashes sometimes when accessing the constants.
Though I find it really weird, since my app is rather small, but may be the Constants class - not an activity - is really removed from the memory in certain cases, though I access its constants in all my activities. That's why I would think it should not be removed from memory anyway.
But for sure, my app crashes in certain cases when accessing the Constants.i1 value.
What would be the best way just to declare some constants in a reliable way. (In c-Derivatives there are the easy to use macros) But there is nothing like this in Android.
-> all I need are "reliable" constants in Java...
EDIT:
declaration of GlobalVars class added
public class GlobalVars {
public static Integer gi1;
public static Integer gi2;
}
Many thanks
EDIT: added crash log
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.screens.One_screen}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851) at android.app.ActivityThread.access$1500(ActivityThread.java:132) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:4293) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.xxxx.xxxx.screens.Settings_screen.presentOnScreen(One_screen.java:172) at com.xxxx.xxxx.screens.Settings_screen.onCreate(One_screen.java:49) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794) ... 11 more
and the line 172 in One_screen is:
if (GlobalVars.gi1 == Constants.i1){