Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:50:37+00:00 2026-05-29T08:50:37+00:00

I’m trying to pass an int between two classes using a bundle. I have

  • 0

I’m trying to pass an int between two classes using a bundle. I have looked online and it seems like it should be easy to do, its just not working for me 🙁 any help is much appreciated! I am trying to pass the int life from one page to another in a simple game.

here is the code for the first page where I am passing it from

int life = 5;

....

if(input.equalsIgnoreCase(teststr))
                {
                    Intent i = new Intent("com.name.PAGETWO");
                    i.putExtra("com.name.PAGETWO.life", life);
                    startActivity(i);
                    mpButtonClick.stop();
}

And on the second page, trying to receive the int

Intent i = new Intent("com.name.PAGETWO");
Bundle extras = i.getExtras();
int temp2 = extras.getInt("life");
int life = temp2;

when i try to run it, it tries to go on to the second page and then just says the application has unexpectedly stopped, but when i comment out the code it continues running as normal, so i know i am definitely putting in the code here wrong somewhere. thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:50:38+00:00Added an answer on May 29, 2026 at 8:50 am

    You’re creating a brand new Intent on the second page, which of course doesn’t have the bundle. Instead, you need to retrieve the calling Intent using getIntent().

    Here’s how your second page’s code could look:

    int life = getIntent().getExtras().getInt("life");
    

    EDIT: Looking at your code again, make sure the key name of the extra is consistent on both ends. To be sure, I usually use a final variable for this. So in your second activity (let’s say it’s ActivityTwo), declare this at the top (outside of onCreate()):

    final static public String EXTRA_LIFE = "life";
    

    Then in the calling activity:

    Intent i = new Intent(this, ActivityTwo.class);
    i.putExtra(ActivityTwo.EXTRA_LIFE, life);
    startActivity(i);
    

    (You may have also been constructing your Intent incorrectly; compare my example).

    And in the second activity, inside of onCreate():

    int life = getIntent().getIntExtra(EXTRA_LIFE, 0);
    

    (You can replace 0 with what you want the default value to be if the extra doesn’t exist.)

    That way you don’t have to worry about typos and can rely on Eclipse’s suggestions to make sure you’re consistent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.