无法在onclick方法中访问变量 [英] unable to access variable in onclick method
本文介绍了无法在onclick方法中访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在设置一个onclick方法,我无法访问我在其父方法中设置的int,是否有不同的方式来使用Int?
I'm setting up an onclick method and i cannot access an int that i have set up in its parent method, is there a different way to use the Int?
这是代码:
Date past = new Date(cYear, cMonth, cDay); // current Date
Date today = new Date(mYear, mMonth, mDay); // date Choosen by the user
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays()+1;
mDateDisplay.setText(""+days);
// display the current date (this method is below)
updateDisplay();
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
s = WorkoutChoice.this.weight.getText().toString();
s2 = WorkoutChoice.this.height.getText().toString();
int w = Integer.parseInt(s);
double h = Double.parseDouble(s2);
double BMI = (w/h)/h;
t.setText(""+BMI);
id = db.insertTitle("001", ""+days, ""+BMI)
}
});
推荐答案
为了从内部类引用变量,你需要使变量 final
:
In order to refer the variable from an inner class, you need to make the variable final
:
final int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays()+1;
这篇关于无法在onclick方法中访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文