2016-06-07 62 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);