unable to access variable in onclick method

蓝咒 提交于 2020-01-25 05:43:10

问题


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?

here is the code:

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)
            }
        });

回答1:


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;


来源:https://stackoverflow.com/questions/9960215/unable-to-access-variable-in-onclick-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!