1

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 :)

7
  • 4
    Hmmm I suggest you first read up on Java programming in general. You're looking for "instance variables", by the way. Commented Dec 14, 2012 at 16:49
  • Declare it as class variable and post the logcat crash Commented Dec 14, 2012 at 16:49
  • nothing is right i this code you have declared myRanNumber1 double but later assigning int to it , addition1 method already return double but you are casting again double to double Commented Dec 14, 2012 at 16:55
  • retagged. it is not Android related. Commented Dec 14, 2012 at 16:57
  • 1
    @WebnetMobile.com, it's android related. Activity & onCreate Commented Dec 14, 2012 at 16:58

2 Answers 2

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use The Key code....... You can store value with key and getValue with key

    Intent i = new Intent();
    i.putExtra("key", doubleValue);

    i.getDoubleExtra("key", defValue);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.