2016-06-07 173 views
0

我的方法newNum帶參數resetB正在一個onClick按鈕上調用,但我也希望它在onCreate中運行。由於某種原因,當我嘗試在onCreate中調用newNum(resetB);時,它給了我錯誤:找不到符號變量resetB。我不明白這是爲什麼。任何幫助,將不勝感激!找不到符號變量(Android Studio)

public void newNum(View resetB){ 
    TextView numOne = (TextView) findViewById(R.id.numOne); 
    TextView numTwo = (TextView) findViewById(R.id.numTwo); 
    TextView ans = (TextView) findViewById(R.id.ans); 
    Button reset = (Button) resetB; 

    Random rand = new Random(); 
    int ranOne = rand.nextInt(50) + 1; 
    int ranTwo = rand.nextInt(50) + 1; 
    int ansNum = (ranOne/2) + ranTwo; 

    numOne.setText("" + ranOne); 
    numTwo.setText("" + ranTwo); 
    ans.setText("" + ansNum); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    newNum(resetB); 
} 

回答

1

變量resetB未在範圍onCreate中定義。試試:

View reset = findViewById(R.id.<>); // Put the id of the button with the onClick attribute 
newNum(reset);