我是 android 编程的新手,但我似乎无法在网上找到一些小东西。我的问题是我想在另一个方法中使用 onCreate 方法中的变量。我确定这是在某个地方在线,但无论我搜索什么,我都找不到灵魂。也许我的措辞都是错误的......我想重新使用 onCreate 中的双打来执行 IF 语句,看看答案是否正确。应用程序此时崩溃..
这是我的代码:
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);
}
非常感谢任何帮助:)