2

这是我的代码: 在 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);      
            }
        };
4

1 回答 1

2

移动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不需要修改器来编译代码。

于 2013-10-10T03:29:41.263 回答