Victor Escobar
Victor Escobar

Reputation: 554

NullPointerException when i tried to get Extras in FragmentActivity

i have a problem to pass extras between an Activity and a FragmentActivity.

This next code create the Intent to start the new Activity:

Intent fichaSerie = new Intent(getActivity(),ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getID());
getActivity().startActivity(fichaSerie);

When I tried to get the Extras in the Fragment Activity, i get a NullPointerException:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
String extra = getIntent().getExtras().getString("serie") //NullPointerException
}

Anyone know what mistake(s) i'm made in my code?

Thank you very much to all.

Upvotes: 0

Views: 2802

Answers (3)

Victor Escobar
Victor Escobar

Reputation: 554

Thanks all your help guys, i solved my problem using this call:

getIntent().getStringExtra("serie")

I mark this question as solved.

Thank you again guys.

Upvotes: 3

user1496130
user1496130

Reputation:

You are put an Integer value and get it into the String value so that it gets a NullPointerException.

Upvotes: 0

PacQ
PacQ

Reputation: 334

First of all Java is case sensitive so .getID() is not the same as .getId(). If you are trying to get an ID of an objet, use .getId(). That usually returns an Integer value

Pay attention on the type you are putting to extra as Michal said (String or Integer or anything else)

Intent fichaSerie = new Intent(getApplicationContext(), ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getId()); //presuming this id is an Integer or long int ... You could change this to string if it is still usable then (serie.getId().toString())... but you have to get an intent with myIntent.getStringExtra(...), but I think you will not be doing this.
startActivityForResult(fichaSerie, 0);

Getting an extra from ActividadSerie.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
Intent myIntent = getIntent();
String extra = myIntent.getStringExtra("serie"); //try diferent gets... getIntExtra(), getStringExtra()..
}

Upvotes: 1

Related Questions