我的带有参数resetB的方法newNum正在按钮onClick上调用,但我也希望它在onCreate中运行。由于某些原因,当我尝试在onCreate上调用newNum(resetB);
时,出现了以下错误:找不到符号变量resetB 。我不明白为什么会这样。任何帮助,将不胜感激!
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);
}
最佳答案
未在范围onCreate
中定义变量resetB。尝试:
View reset = findViewById(R.id.<>); // Put the id of the button with the onClick attribute
newNum( reset );
关于android - 找不到 View 的符号变量(Android Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37689527/