我有一个带有线程(闹钟)的主屏幕,并且可以启动带有PreferentsFragments的设置屏幕活动。当应用程序使用时间较长而用户更改回应用程序时,我会崩溃。一些变量为空。这些是在另一个类中声明的静态变量,供所有活动使用。
主屏幕:
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;
...
}
应用设置:
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 !!!
.....
}
我的猜测是操作系统正在销毁我的活动,然后只恢复设置活动,但Tools.java由主活动初始化,然后一切都为空。
这是我从另一个PreferenceActivity崩溃的日志。
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
答案 0 :(得分:0)
您应该在当前活动回调 onSaveInstanceState 中保存变量的状态,并在 onRestoreInstanceState 中检索其值,如此处所述( how-to-use-onsaveinstancestate-and-onrestoreinstancestate) < /强>