我有两个活动,第一个是LoginActivity,其中我有五个静态变量:
public static Login loginTargetApp;
public static Gateway gateway;
public static Login loginGateway;
public static OfflineUser offlineUser;
public static boolean isLoggedIn;
在我的第二个活动MainViewActivity中,当我尝试在android销毁并重新处理我的活动后访问此变量时,它返回null并且应用程序崩溃:
jsonTempDownload = new File(Constants.DEST_PATH_PARENT + File.separator
+ LoginActivity.gateway.getUuidInstituicao() + Constants.JSON_TEMP_DOWNLOAD);
我正在尝试保存变量状态:
@Override
protected void onSaveInstanceState(Bundle saveInstanceState) {
saveInstanceState.putParcelable("loginTargetApp", LoginActivity.loginTargetApp);
saveInstanceState.putParcelable("gateway", LoginActivity.gateway);
saveInstanceState.putParcelable("loginGateway", LoginActivity.loginGateway);
saveInstanceState.putParcelable("offlineUser", LoginActivity.offlineUser);
saveInstanceState.putBoolean("isLoggedIn", LoginActivity.isLoggedIn);
super.onSaveInstanceState(saveInstanceState);
}
要恢复我的变量值,我尝试了两种不同的方法,但它们不起作用:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
LoginActivity.loginTargetApp = savedInstanceState.getParcelable("loginTargetApp");
LoginActivity.gateway = savedInstanceState.getParcelable("gateway");
LoginActivity.loginGateway = savedInstanceState.getParcelable("loginGateway");
LoginActivity.offlineUser = savedInstanceState.getParcelable("offlineUser");
LoginActivity.isLoggedIn = savedInstanceState.getBoolean("isLoggedIn");
}
和
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Constants.MAIN_VIEW_ACTIVITY, "----------------");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "Abrindo MainViewActivity...");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "----------------");
if (savedInstanceState != null) {
// Restore value of members from saved state
LoginActivity.isLoggedIn = savedInstanceState.getBoolean("isLoggedIn");
boolean isLoggedIn = savedInstanceState.getBoolean("isLoggedIn");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL NORMAL: " + isLoggedIn);
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL ESTÁTICA: " + LoginActivity.isLoggedIn);
LoginActivity.loginTargetApp = savedInstanceState.getParcelable("loginTargetApp");
Login loginTargetApp = savedInstanceState.getParcelable("loginTargetApp");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL NORMAL: " + loginTargetApp.getEmailAndress());
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL ESTÁTICA: " + LoginActivity.loginTargetApp.getEmailAndress());
LoginActivity.gateway = savedInstanceState.getParcelable("gateway");
LoginActivity.loginGateway = savedInstanceState.getParcelable("loginGateway");
LoginActivity.offlineUser = savedInstanceState.getParcelable("offlineUser");
OfflineUser offlineUser = savedInstanceState.getParcelable("offlineUser");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL NORMAL: " + offlineUser.getEmail());
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL ESTÁTICA: " + LoginActivity.offlineUser.getEmail());
} else {
Log.d(Constants.MAIN_VIEW_ACTIVITY, "SAVED INSTANCE STATE IS NULL");
}
在onCreate方法中,我的savedInstance不为null但是当我尝试在Log.d中显示值时,它仍然会崩溃。
答案 0 :(得分:1)
几点:
1- static
不是final
,因此可以为null,换句话说,请确保将这些值设置为某些值。
2- super.onRestoreInstanceState(savedInstanceState);
应在protected void onRestoreInstanceState(Bundle savedInstanceState)
开头而不是结束时调用。
Source (Look at the end of the page)
让我知道,欢呼。