0

I have a main screen with a thread (alarm clock) and a settings screen activity with PreferentsFragments can be started. When the app is not used for longer time and user changes back to app then I get a crash. Some variables are null. These are static variables declared in another class which are used by all activites.

Main Screen:

public class FullscreenActivity extends BaseAppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    Tools.mAppSettings = new AppSettings(this);
    ...
}

Tools.java

public class Tools {
    static public BrightnessManager brightnessManager;
    static public SoundManager soundManager;
    static public AppSettings mAppSettings;
    static public AlarmMan alarmMan;
    static public Vibrator vibrator;
    ...
}

App Settings:

public class AppSettingsActivity extends BaseSettingsActivity {

SettingsFragment prefFragment; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefFragment = new SettingsFragment();
        prefFragment.context = this;
        addFragment(prefFragment);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        Tools.grantIntentIfUsesAnyDeviceBrightnessNotAllowed(this, new GrantIntentListener() {
            @Override
            public void intentIfNeeded(boolean intentNeeded, Intent grantIntent) {
                if (intentNeeded) {
                    startActivityForResult(grantIntent, ASK_WRITE_PERMISSION);
                }
            }
        });
    }

    public static class SettingsFragment extends PreferenceFragment {
        public static Context context;
        Preference unlock;
        PreferenceCategory about;

        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_settings);

            MultiSelectListPreference clockInfo = ((MultiSelectListPreference) findPreference("clockInfo"));
            Set<String> clockInfoSet = new HashSet<String>();

                if (Tools.mAppSettings.showDate)
                    clockInfoSet.add("showDate");
>>>>>>>>>>>  Tools.mAppSettings is null sometimes !!!
.....
}

My guess is that the OS is destroying my activities and then only restoring the settings activity but Tools.java is initialized by main activity and then everything is null.

  • Is my concept with having static variables in a Tools.java for sharing data between different activies not feasabile for Android? Do I need to pass "everything" with intent.putExtra?
  • Do I need to implement Bundle savedInstanceState?

Here is a log of the crash from another PreferenceActivity I have.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.changemystyle.gentlewakeup/com.changemystyle.gentlewakeup.settings.LightSettingsActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
    at android.app.ActivityThread.access$900(ActivityThread.java:172)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.changemystyle.gentlewakeup.settings.LightSettingsActivity$LightFragment.updateUI(LightSettingsActivity.java:192)
    at com.changemystyle.gentlewakeup.settings.LightSettingsActivity$LightFragment.onCreate(LightSettingsActivity.java:177)
    at android.app.Fragment.performCreate(Fragment.java:1678)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
    at android.app.Activity.performStart(Activity.java:5550)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
    ... 11 more
Alex
  • 343
  • 2
  • 13
  • uh.. what is Main? You're not following very good programming practice in terms of android. You should probably initialize your class in the onCreate of your Activity. Also what does the constructor of AppSettings do? – JoxTraex Sep 05 '16 at 06:48
  • please share your log when your app is crash. – Farmer Sep 05 '16 at 07:02
  • I simplified the code for presentation, there is no Main. I corrected in the post. – Alex Sep 06 '16 at 13:45

1 Answers1

0

You should save the state of your Variables in the Current Activity CallBack onSaveInstanceState and retrieve their values in onRestoreInstanceState as stated here (how-to-use-onsaveinstancestate-and-onrestoreinstancestate).

Community
  • 1
  • 1
  • Yes, but these are static variables used in Tools.java also from the Main Screen. If these variables are not there any more what does this mean for my main screen? Is it gone as well? I do not understand the workflow there. When you click on settings on mainscreen and App Settings are opened, is the main screen killed completely? Because the variables in Tools.java are still valid. So if in one case variables remains but on other case they do not than the whole concept of having static variables in a Tools.java is not feasable. – Alex Sep 06 '16 at 13:54
  • And what exactly is happening in the case where variables are null? Is the OS deciding to destroy everything but when switching back to App it is starting with settings screen? – Alex Sep 06 '16 at 13:54
  • Sometimes application is not destroyed completely and remains in the Application Stack resulting in the killing of Process. When the process is recreated, the static variables are assigned to their default values. [See References](http://stackoverflow.com/questions/17900456/lifetime-of-a-static-variable-in-android). Also cross-check whether the code has parts to perform re-initialization of your variables. – Rishabh Dutt Sharma Sep 07 '16 at 07:00