当我在OnClickListener
中访问我的变量时,它显示错误。
这是一段代码:
EditText hight=(EditText)findViewById(R.id.hight);
EditText wight=(EditText)findViewById(R.id.wight);
Button calc=(Button) findViewById(R.id.calculate);
TextView result=(TextView) findViewById(R.id.result);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double h=Double.valueOf(hight.getText().toString());
Double w=Double.valueOf(wight.getText().toString());
h=h/100;
答案 0 :(得分:0)
如果您想在内部类中使用它们,那么您尝试访问的变量只需要标记为final。
final EditText hight=(EditText)findViewById(R.id.hight);
final EditText wight=(EditText)findViewById(R.id.wight);
final Button calc=(Button) findViewById(R.id.calculate);
final TextView result=(TextView) findViewById(R.id.result);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double h=Double.valueOf(hight.getText().toString());
Double w=Double.valueOf(wight.getText().toString());
h=h/100;
答案 1 :(得分:0)
试试这个:
final EditText hight,wight;
final Button calc;
final TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hight=(EditText)findViewById(R.id.hight);
wight=(EditText)findViewById(R.id.wight);
calc=(Button) findViewById(R.id.calculate);
result=(TextView) findViewById(R.id.result);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double h=Double.valueOf(hight.getText().toString());
Double w=Double.valueOf(wight.getText().toString());
h=h/100;
答案 2 :(得分:0)
final EditText hight=(EditText)findViewById(R.id.hight);
final EditText wight=(EditText)findViewById(R.id.wight);
Button calc=(Button) findViewById(R.id.calculate);
TextView result=(TextView) findViewById(R.id.result);
无论您要在内部课程中访问什么,您都必须将这些项目声明为最终项目。