无法解析DateSetListener对象

时间:2013-10-10 03:24:34

标签: java android

这是我的代码:在onClick()方法中声明的datepickobj似乎无法识别在DatePickerDialog.OnDateSetListener中创建的对象。 我得到'datepickobj无法解析变量'错误!任何帮助非常感谢。

protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);
        userdate =(EditText) findViewById(R.id.user_datespent);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date da = new Date();
        userdate.setText( sdf.format(da));
        final Calendar cal = Calendar.getInstance();    



        Button changeDate = (Button) findViewById(R.id.button_for_cal);
        changeDate.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 DatePickerDialog(this, datepickobj,  cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 
                        cal.get(Calendar.DAY_OF_MONTH)).show();
                userdate.setText("");
            }
        });

        DatePickerDialog.OnDateSetListener datepickobj = new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                cday = dayOfMonth;
                cmonth = monthOfYear + 1;
                cyear = year;

                userdate.setText("Choosen date is :" + cday + "/" + cmonth + "/" + cyear);      
            }
        };

1 个答案:

答案 0 :(得分:1)

移动datepickobj使其位于行

之前
changeDate.setOnClickListener(new OnClickListener() {  

并将其声明为final

protected void onCreate(Bundle savedInstanceState) {  
  // TODO Auto-generated method stub  
  super.onCreate(savedInstanceState);
  userdate =(EditText) findViewById(R.id.user_datespent);
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  Date da = new Date();
  userdate.setText( sdf.format(da));
  final Calendar cal = Calendar.getInstance();    

  final DatePickerDialog.OnDateSetListener datepickobj = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
      // TODO Auto-generated method stub
      cday = dayOfMonth;
      cmonth = monthOfYear + 1;
      cyear = year;

      userdate.setText("Choosen date is :" + cday + "/" + cmonth + "/" + cyear);      
    }
  };

  Button changeDate = (Button) findViewById(R.id.button_for_cal);
  changeDate.setOnClickListener(new OnClickListener() {           
    @Override
    public void onClick(View v) {
      // TODO Auto-generated method stub
      DatePickerDialog(this, datepickobj,  cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 
                       cal.get(Calendar.DAY_OF_MONTH)).show();
      userdate.setText("");
    }
  });
}

Java中的方法范围变量遵循声明方面的线性模式,您不能在方法的末尾声明变量并尝试在任何地方使用它。

使用final修饰符是因为您在匿名内部类中使用datepickobj。如果你使datepickobj成为一个实例变量(也就是说,在任何方法之外,在类的根处),则不需要final修饰符来获取要编译的代码。