-2

When I create a new Intent in an Activity:

Intent myIntent = new Intent(this, TargetActivity.class);
// apply hacks explained in above stackoverflow answers
System.out.println(myIntent.getExtras())

(this code lays in a default blank activity)

Intent#getExtras() is always null and thus Intent#putExtras(...) leads to immediate NullPointerException. Starting the Intent, however, works without any problems.

I've read all similar stackoverflow posts about this topic:

I've tried them all, but none of them seems to have any effect.

Could anyone please provide a solution to simply start an Intent with non-null extras which always works?

Alternatively, is there any other way to provide extra information to an Intent which is clean and not leaky?

Community
  • 1
  • 1
SputNick
  • 1,231
  • 5
  • 15
  • 26
  • `thus Intent#putExtras(...) leads to immediate NullPointerException`. I don't think so. The source for `Intent` has a check for null and initialize the Extras when you first call `putExtras` – njzk2 Jun 26 '15 at 19:05
  • What did you expect ? you call intent.getExtras before adding any data to the Intent.. – Ofir Ohayon Jun 26 '15 at 19:07
  • None of those links you posted seem to apply directly to just trying to pass an extra in an Intent from one Activity to another. Try this: http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android/7325248#7325248 – Daniel Nugent Jun 26 '15 at 19:22

1 Answers1

0

OK, I found the problem. Thank you @DanielNugent for pointing to the canonical answer to Intent handling.

The problem was: when I did

myIntent .putExtra("myVar", myVar);

myVar was null!

Stupid of me I didn't double check that and panic, put somehow stupid of Android that this does indeed lead to immediate NullPointerException! Please try it yourself if you don't believe me.

@njzk2: Thank you, I didn't realize that.

TylerH
  • 20,799
  • 66
  • 75
  • 101
SputNick
  • 1,231
  • 5
  • 15
  • 26