Getintent() Extras Always Null

getIntent().getExtras() returns null Android

Thanks to everyone, all the answers moved me closer to making it work but I needed to change the PendingIntent to the following to nail it.

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Thanks...<3

Bundle = getIntent().getExtras() is always null

You're using getIntent() instead of the Intent data... So, that's not the intent that you have passed via startActivityForResult

For example

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
String previousActivity = extras.getString("FROM_ACTIVITY");

Also, if getIntent().getExtras() works outside of the click event, then make it a field.

private Bundle extras;

public void onCreate(Bundle savedInstanceState) {
....
this.extras = getIntent().getExtras();
}

And use those in the onClick

Ideally, though, I'd suggest not defining the entire adapter within the Activity code and instead create a separate class file. In which case, you cannot use the Activity Intent within the adapter code

(Activity(context)).getIntent().getExtras() return null

Try stepping through this under the debugger:

public class Request {
private Context context;
...
public Request(Context context) {
this.context = context;
try {
Intent intent = context.getIntent();
Bundle extras = intent.getExtras(); // <-- I'll betcha' this is returning "null"
this.arguments = new Arguments(extras);
...
} catch (Exception e) {
Log.i("output", "no arguments has been found " + e.toString());
}
...

Nullpointer on getIntent().getExtras();

when you call getIntent(), it returns the intent that you created in previous Activity.

public Intent getIntent ()

Return the intent that started this activity.

Activity documentation

so in second activity, your intent is null

in onCreate initiate your intent

intent.getStringExtra is always null

You are creating a brand new intent in the other activity, so it has no extras. In the new activity you need to call getIntent(), like this

Intent intent = getIntent(); // don't use 'new Intent()' here
String username = intent.getStringExtra("username");
Log.i("test3","username: "+username);

Note that calling getStringExtra just calls getExtras() internally, then getString(...), so it is the same as using getIntent().getExtras().getString(...)

Intent.getExtras() always returns null

I'm going to lean out the window and guess that your problem is here:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

You are passing intent to getActivity() and expecting that you will get back a PendingIntent that matches your Intent and includes your extras. Unfortunately, if there is already a PendingIntent floating around in the system that matches your Intent (without taking into consideration your Intent extras) then getActivity() will return you that PendingIntent instead.

To see if this is the problem, try this:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

This says that if there is already a PendingIntent that matches your Intent somewhere in the system that it should replace the extras with the ones in your intent parameter.



Related Topics



Leave a reply



Submit