The difference between getExtras (name) .getString and getIntent (). GetStringExtra (name)

I have error messages in an Android app, this is a Nullpointerexception in onCreate () in Activity. Invalid code: getIntent (). getExtras (). getStringExtra ("name"). (Nullpointerexception)

This means getExtras () is null somehow. I am sure that I set an intention additionally in every place that I create. I can not recreate it on the emulator on the device. I think this happened on my real device (but not during debugging) after I tried to open the application again, meanwhile Android probably killed the process and restored activity again. But shouldn't there be intentions left even in this scenario?

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

I replaced the getIntent () code. getStringExtra (). What difference, moreover, would not throw a nullpointerexception, but would still set String to null. Is there any other difference?

What could be the reason for this?

+6
source share
3 answers

I found out that somewhere else in my code I was creating shortcuts that had the string [] extra. Android HOME (and possibly other parts of the system) do not store string arrays, only primitive additions (int, string, long, float ...). But the code causing the problem does not use shortcuts, it is just a simple operation that gets an extra array of strings. Perhaps empty additional functions were caused by this problem - the application was killed by the OS, and additional data could not be saved. In this case, Android is no exception.

Described by Romain Guy here: http://groups.google.com/group/android-developers/browse_thread/thread/7f2ce458bd5d112f/189e2b7b2b2532d7

I asked a more specific question about this here: Quick access utilities lost after reboot?

0
source

Intent.getStringExtra () returns null if there are no additional parameters. Intent.getExtras () returns null if there are no additional components, so you need to check this before trying to call getString () or other methods on it.

+8
source

getIntent.getExtras () returns zero at some point. getIntent (). getStringExtra () is most likely encoded to check for null padding and return null if getExtras () is null. The so-called "no-throw" architecture. If so, this is the actual execution at runtime and will not throw an exception. a call to getStringExtra in null padding is not valid at run time and should throw an exception.

There seems to be a way where getExtras () is NULL, so you can encode it by checking for zero extra functions.

0
source

Source: https://habr.com/ru/post/887072/


All Articles