0

Hi I've been trying to get this to work in my game but it won't work. I can get lives key to reappear in the second activity but the gold key won't work. Here is my first activity.

 Intent intent = new Intent(getApplicationContext(),EndActivity.class);
                        intent.putExtra("lives", lives);
                        intent.putExtra("gold", gold);
                        startActivity(intent);

My 2nd Activity

Intent extras = getIntent();
    int gold = extras.getExtras().getInt("gold", 0);
    int lives = extras.getExtras().getInt("lives", 0);

am i missing anything? I've check my spelling. I tried to see if only 1 key is allowed in Intent. I've checked other questions but I can't find any that helps.

3 Answers3

0

First,be sure that lives and gold are int when you do this:

intent.putExtra("lives", lives); intent.putExtra("gold", gold);

Second,you have to use this code in second Activity:

Bundle extras = getIntent().getExtras();

if (extras.containsKey("lives") && extras.containsKey("lives") )
{
 int gold = extras.getExtras().getInt("gold";
 int lives = extras.getExtras().getInt("lives");
}

Also, pay attention on this:

Intent intent = new Intent(FirstActivity.this,EndActivity.class);

So you have to use FirstActivity.this instead of getApplicationContext();

Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
  • aaaaaaaaa im soo stupid!!! I had gold as long!!! I turned it into int and its all fine now! I had to remake my app after it got deleted and it worked in previous version! I guess I changed it to int then but forgot about it! Thank you!! – noni khanna Aug 05 '14 at 06:56
0

try:

int gold = Integer.parseInt(extras.getString("gold"));

or

int gold = extras.getInt("gold");
Nabin
  • 11,216
  • 8
  • 63
  • 98
0

use this :

 Intent i = new Intent(first.this, EndActivity.class);
           Bundle bundle = new Bundle();
           bundle.putInt("gold", gold);
           bundle.putInt("gold", gold);
           i.putExtras(bundle);
           startActivity(i);

end activity :

Bundle getData = getIntent().getExtras();
        int gold = getData.getInt("gold");
         int lives = getData.getInt("lives");
Meysam
  • 694
  • 6
  • 20