0

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.

moondaisy
  • 4,303
  • 6
  • 41
  • 70
  • Is there an existing instance of `MyOfferActivity` around at the time you are starting it with this code? And are you using `android:launchMode` or other things in your manifest to reuse that existing instance? – CommonsWare May 24 '17 at 20:26
  • I'm not using `android:launchMode` but I'm not sure where to look to see if there is an existing instance of `MyOfferActivity` (I'm using Android Studio) – moondaisy May 24 '17 at 20:34
  • "I'm not sure where to look to see if there is an existing instance" -- are you starting `MyOfferActivity` from any other place in your code? Is there a scenario where you are starting `MyOfferActivity` (from this code or other code), then navigating away from it (e.g., starting some other activity without finishing the `MyOfferActivity`), then starting `MyOfferActivity` again? – CommonsWare May 24 '17 at 20:35
  • No, this is the first time I start the activity since I open the app – moondaisy May 24 '17 at 20:38
  • 2
    Replace `intent.putExtra(Constants.OFFER_INTENT_KEY, offer);` with `intent.putExtra(Constants.OFFER_INTENT_KEY, 123);`, and see if you get `123` in `MyOfferActivity`. If the answer is yes, then you have some serialization issue with `Offer`. – CommonsWare May 24 '17 at 20:46
  • I guess that is the case then, I will check that and update if it's still broken – moondaisy May 24 '17 at 20:55

2 Answers2

0

Your second option should work just fine.

Replace

extras.putSerializable(Constants.OFFER_INTENT_KEY, offer);
extras.putSerializable(Constants.USER_INTENT_KEY, user);

with

intent.putExtra(Constants.OFFER_INTENT_KEY, offer);
intent.putExtra(Constants.USER_INTENT_KEY, user);

And on your activity

offer = (Offer) getIntent().getSerializableExtra(Constants.OFFER_INTENT_KEY);
user = (User) getIntent().getSerializableExtra(Constants.USER_INTENT_KEY);
Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20
-1

Hello @moondaisy, I will suggest you to pass the data using Parcelable interface. Here is the link https://developer.android.com/reference/android/os/Parcelable.html Also check this How can I make my custom objects Parcelable?

Parcelable interface is more faster than Serilization making your app efficient.

mdb
  • 166
  • 8