Mike
Mike

Reputation: 6839

Android extra with intent is always NULL

For some reason I am having trouble retrieving my bundle extras from my intent. I am adding my extras to pass to my new activity with:

public void getMap(View view){

        Intent i = new Intent(this, BreweryYourTopBeers.class);
        Log.d("map" , e.beerBreweryId);
        i.putExtra("breweryID",  e.beerBreweryId);
        i.setClass(this, BreweryMap.class);

        startActivity(i);


    }

Looking at my LOG I can see that e.beerBreweryId does in fact have a value before it is bundled and sent.

But when I get to the next class I must not be retrieving the extras from my intent. I attempt to retrieve the value with:

public class BreweryMap extends ActionbarMenu {

    BeerData e;
    String beerID;
    GoogleMap map;

    //get beer details from bundle
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brewerymap);

        //get brewery data

        //get beer data
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String breweryID = extras.getString("brewreyID");


        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();



        //todo: get brewery latt and long and add marker
        //construct url
        String url = "http://api.brewerydb.com/v2/brewery/"+ breweryID +"?key=myKey&format=json&withLocations=y";

        Log.d("map", url);
        //async task to get beer taste tag percents
        new myAsyncTask(this,map).execute(url);

}

When I look at my log here the breweryID is always NULL and I can not figure out why.

Upvotes: 2

Views: 3492

Answers (3)

Teovald
Teovald

Reputation: 4389

As @Rick Royd Aban said, you have a typo in your code with switched e & r in one string.

this is why you should never use strings directly but rather do something like this:

public static final String EXTRA_BREWERY_ID = "brewery_id";
i.putExtra(EXTRA_BREWERY_ID,  e.beerBreweryId);
String breweryID = extras.getString(EXTRA_BREWERY_ID);

This way : if you make a typo, your code won't compile, way better than to have to track the bug and it makes reusing it way easier and maintenable.

Upvotes: 3

pedja
pedja

Reputation: 3413

Intent intent = getIntent();  
String breweryID = intent.getStringExtra("brewreyID");

Upvotes: 0

Rick Royd Aban
Rick Royd Aban

Reputation: 904

You got typographical error there

Try changing this line from

String breweryID = extras.getString("brewreyID");

to

String breweryID = extras.getString("breweryID");

Upvotes: 3

Related Questions