DatePickerDialog中的异常

时间:2011-01-10 06:29:28

标签: android exception datepicker

我正在尝试在类'dateDisplay.class'中创建一个DatePickerDialog。我从'main.class'调用此活动。如果我使用startActivity()调用'dateDisplay.class',那么DatePickerDialog工作正常。但实际上我使用的是ActivityGroup(在我的应用程序中使用tab),我使用以下代码启动'dateDisplay.class':

    Intent dateIntent=new Intent(context,dateDisplay.class);
    View v=getLocalActivityManager().startActivity("2",dateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    setContentView(v);

但是在调用onCreateDialog()函数时会捕获异常。这个过程突然停止了。它显示发生了TargetInvocationException。我该如何更正代码? 以下是我的代码:

public  Dialog onCreateDialog(int id,Bundle b)
     {
       Calendar c=Calendar.getInstance();
       int day=c.get(Calendar.DAY_OF_MONTH);
       int month=c.get(Calendar.MONTH);
       int year=c.get(Calendar.YEAR);
       Dialog d = null;
       if(id==DATE_DIALOG_ID)
       {
          return    new DatePickerDialog(this,dateChangeListener,year,month,day); 
       }

       else
       {
               return null;
         }


     }

请帮帮我.. 谢谢..

1 个答案:

答案 0 :(得分:1)

只需使用此代码即可解决问题

 protected Dialog onCreateDialog(int id) {

            Calendar c = Calendar.getInstance();
            int cyear = c.get(Calendar.YEAR);
            int cmonth = c.get(Calendar.MONTH);
            int cday = c.get(Calendar.DAY_OF_MONTH);
            switch (id) {
            case 0:

            return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
            }
            return null;
            }

            private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            String date_selected = String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);

            EditText birthday=(EditText) findViewById(R.id.EditTextBirthday);
            birthday.setText(date_selected);
            }
            };
            public void onClick(View v) {
            if(v == ((ImageView) findViewById(R.id.pickDate)))
            showDialog(0);

            }

        }