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 8985323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:18:18+00:00 2026-06-15T21:18:18+00:00

I am new to android programming but there is something small that i can’t

  • 0

I am new to android programming but there is something small that i can’t seem to find online.
My problem is i want to use a variable from the onCreate method in another method.
Im sure this is online somewhere but whatever i have searched i couldnt find a soultion. Maybe i am phrasing this all wrong….
I want to re-use the doubles in the onCreate to perform an IF statement to see if the answer is correct. App crashes at the moment..

Here is my code:

 public class Addition extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_addition);

    View results = findViewById(R.id.check_button1);


//set event listener
    results.setOnClickListener(this);


        double myRanNumber1; {

        // variable we will use to store a random number
        myRanNumber1 = Math.random(); 
        //myRanNumber generated between 0.000000... & 9.9999...
        myRanNumber1 = myRanNumber1 *10; 
        // myRanNumber between 0.00.. and 9.999..
        myRanNumber1 = (int) myRanNumber1;// myRanNumber between 0 and 9 (fraction chopped off)
        double myRanNumber=(myRanNumber1);
        DecimalFormat df = new DecimalFormat("####");
        String number = df.format(myRanNumber);
        String printnumber = number;
        TextView random = (TextView) findViewById(R.id.TextViewRand);
        random.setText(printnumber); 
        }

         double myRanNumber2; 
         {
        // variable we will use to store a random number
        myRanNumber2 = Math.random(); 
        //myRanNumber generated between 0.000000... & 9.9999...
        myRanNumber2 = myRanNumber2 *10; 
        // myRanNumber between 0.00.. and 9.999..
        myRanNumber2 = (int) myRanNumber2;// myRanNumber between 0 and 9 (fraction chopped off)
        double myRanNumber=(myRanNumber2);
        DecimalFormat df = new DecimalFormat("####");
        String number = df.format(myRanNumber);
        String printnumber = number;
        TextView random = (TextView) findViewById(R.id.TextViewRand1);
        random.setText(printnumber); 
        }

         double additionRes1 = addition1(myRanNumber1, myRanNumber2);
         Intent theIntent = new Intent(this, Results.class);


         Bundle b = new Bundle();
         b.putDouble("key", additionRes1);
         theIntent.putExtras(b);
         startActivity(theIntent);

}

private double addition1 (double myRanNumber1, double myRanNumber2)

{

    return (double) (myRanNumber1 + myRanNumber2);
}

any help be very much appriciated 🙂

  • 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-06-15T21:18:19+00:00Added an answer on June 15, 2026 at 9:18 pm

    Basic scoping of variables requires that you declare them as classwide variables if you want to manipulate them from different functions.

    public class MainActivity extends Activity {
    
        //Declare a class variable to use in this class
        public Double myNumber1;
        public Double myNumber2;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                myNumber1 = 250;
                myNumber2 = 10;
    
                //mySum is only available for use within this method
                Double mySum = addition1 (myNumber1, myNumber2);
    
        }
        private double addition1 (double myRanNumber1, double myRanNumber2) {
           return myRanNumber1 + myRanNumber2;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new at android programming and I've got a problem with orientation. I want
I'm fairly new to Android programming, so this may be a simple question, but
I'm new to Android & Java programming but have been programming in .NET for
I'm fairly new to Java and Android programming in general but I would like
I'm new to Java but not to programming. I'm reading the book Beginning Android
I'm still new in android programming, what I'm trying to do is, I want
I am fairly new to android programming (but not to Java programming). I have
I'm new to android programming. I would like to make an app widget that
I am new to android programming and development. I am working on app that
I am new to Android but not to programming. I have taken C++ in

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.