Reputation: 13
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 :)
Upvotes: 1
Views: 5903
Reputation: 94
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);
Upvotes: 0
Reputation: 3800
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;
}
}
Upvotes: 4