1

我是 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);
}

非常感谢任何帮助:)

4

2 回答 2

3

如果要从不同的函数中操作变量,则变量的基本作用域要求将它们声明为类范围的变量。

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;
    }
}
于 2012-12-14T16:48:51.267 回答
0

使用密钥代码.......您可以使用密钥存储值并使用密钥获取值

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

    i.getDoubleExtra("key", defValue);
于 2012-12-14T17:16:44.220 回答