James James - 1 year ago 82
Android Question

Updated Object (through setter) not reflected in Android Intent

I am having a problem with sending an Intent from my LoginActivity to my MainActivity. So basically, I create an object that represents a User and it implements

. The only constructor for said object includes two
values for username and password. The object, in total, has five fields (other 3 are just instantiated to null). I have a class variable of this object in my login
. When the user presses a
to login, an
is performed that (if the login is successful), populates these other three fields. When this happens, the user in the login
reflects these updated fields, however when I try to send this
via an
to my main
, only the username and password are maintained. Do you know how this could be?

Code from Login:

public void onPostExecute(String input) {
if (rest instanceof SuccessfulLoginFragment) {
SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("loggedin", true).apply();
editor.putBoolean("fromLogin", true).apply();
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentByTag(TAG_LOGIN_REST_FRAGMENT);
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
Log.d("euDailyCost", eu.getDailyInfo().getCurrentAverageCost());
// here I check that it is updated (and it is)
myIntent.putExtra("USER", eu);

Code from Main:

} else {
FM = getFragmentManager();
final SharedPreferences.Editor editor = sharedPref.edit();
if (sharedPref.getBoolean("fromLogin", true)) {
Log.d("enter", "entered");
eu = getIntent().getExtras().getParcelable("USER");
// did not include Username/password log statement, but it returns correct information
Log.d("costLogin", eu.getDailyInfo().getCurrentAverageCost());
// fails to show updated information


D/euDailyCost: 11.24
D/enter: entered
E/AndroidRuntime: FATAL EXCEPTION: main -> IIntervalData.getCurrentAverageCost()' on a null object reference

Answer Source

I found the error. The three fields that were not instantiated in the constructor were all Parcelables themselves. The error was that they were not being properly parced. Sorry for the confusion. The error was not with the Intent itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download