Update
In the end it was a serialization problem as it was pointed out in the comments.
So, I have this method inside the ViewHolder
of an adapter:
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Offer offer = offers.get(position);
Intent intent = new Intent(context, MyOfferActivity.class);
Bundle extras = new Bundle();
extras.putSerializable(Constants.OFFER_INTENT_KEY, offer);
extras.putSerializable(Constants.USER_INTENT_KEY, user);
intent.putExtras(extras);
context.startActivity(intent);
}
}
And on the onCreateView
of MyOfferActivity
I do this:
Bundle extras = getIntent().getExtras();
offer = (Offer) extras.getSerializable(Constants.OFFER_INTENT_KEY);
user = (User) extras.getSerializable(Constants.USER_INTENT_KEY);
The problem is that offer
ends up null
. I have debug it and when I put it in the bundle it isn't null. I can obtain the user
just fine though.
This was previously working without any trouble and it "magically" stopped working.
Actually, originally I was doing this and since it stopped working out of nowhere I changed it to the previous code to see if that fixed it up. It didn't.
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Offer offer = offers.get(position);
Intent intent = new Intent(context, MyOfferActivity.class);
intent.putExtra(Constants.USER_INTENT_KEY, user);
intent.putExtra(Constants.OFFER_INTENT_KEY, offer);
context.startActivity(intent);
}
}
And on the receiver:
offer = (Offer) getIntent().getSerializableExtra(Constants.OFFER_INTENT_KEY);
user = (User) getIntent().getSerializableExtra(Constants.USER_INTENT_KEY);
What can it be?
I have try this two versions of code and also changing the order in which I set the extras and receive them and it still doesn't work.
Also, getIntent().hasExtra(Constants.OFFER_INTENT_KEY)
return false.
This is the error:
FATAL EXCEPTION: main
Process: com.yournanny, PID: 742
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yournanny/com.yournanny.activities.offers.MyOfferActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.yournanny.models.Nanny com.yournanny.models.Offer.getAcceptedNanny()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2711)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6223)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.yournanny.models.Nanny com.yournanny.models.Offer.getAcceptedNanny()' on a null object reference
at com.yournanny.activities.offers.MyOfferActivity.setContent(MyOfferActivity.java:69)
at com.yournanny.activities.offers.MyOfferActivity.onCreate(MyOfferActivity.java:64)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6223)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
The line causing it is this one:
if (user.getType() == UserType.FAMILY && offer.getAcceptedNanny() == null && offer.getState() == State.REQUESTED)
which is the first one in which I use offer
inside the activity.