James James - 25 days ago 6
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

Parcelable
. The only constructor for said object includes two
String
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
Activity
. When the user presses a
Button
to login, an
AsyncTask
is performed that (if the login is successful), populates these other three fields. When this happens, the user in the login
Activity
reflects these updated fields, however when I try to send this
Object
via an
Intent
to my main
Activity
, only the username and password are maintained. Do you know how this could be?

Code from Login:

@Override
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();
editor.commit();
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentByTag(TAG_LOGIN_REST_FRAGMENT);
fm.beginTransaction().remove(fragment).commit();
fm.executePendingTransactions();
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);
startActivity(myIntent);
}


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
}


Log:

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

Answer

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.