Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getExtras(name).getString and getIntent().getStringExtra(name)

I have some error reports on my Android app, it's a Nullpointerexception in onCreate() in an Activity. The code that fails is getIntent().getExtras().getStringExtra("name"). (Nullpointerexception)

That means that getExtras() is null somehow. I am sure that I am setting the intent extra on every place I am creating the intent. I can't recreate it on my emulator on device. I think it happened on my real device (but not while I was debugging) after I tried to open the app again, in the mean time Android probably killed the process and recreated the activity again. But shouldn't be the intent extras kept even in this scenario?

I tried to kill the process on the emulator, onCreate was called again and the getExtras() returned the right value.

I replaced the code with getIntent().getStringExtra(). What's the difference besides it won't throw a nullpointerexception but will still set the String as null. Is there any other difference?

What could be causing it?

like image 765
Daniel Novak Avatar asked May 01 '11 21:05

Daniel Novak


1 Answers

Intent.getStringExtra() returns null if there are no extras. Intent.getExtras() returns null if there are no extras, so you need to check for that before trying to call getString() or other methods on it.

like image 50
hackbod Avatar answered Nov 12 '22 15:11

hackbod