DatePickerDialog错误:已添加片段

时间:2016-11-07 08:35:53

标签: android datepickerdialog

我的datePickerDialog存在问题。使用EditText作为选择器,当我尝试单击/双击选择器时,它会给我错误。如何防止此错误

 java.lang.IllegalStateException: Fragment already added: DatePickerDialog {4a9aadf4 #0 datePickerDialog1}
  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1361) 
  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:734)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
  at android.os.Handler.handleCallback(Handler.java:733)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5045)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  at dalvik.system.NativeStart.main(Native Method)

我已经尝试在代码上添加datePickerDialog1.isAdded(),但它仍无效

这是我在datePickerDialog

下面的代码
date1 = (EditText) findViewById(R.id.date1);
date2 = (EditText) findViewById(R.id.date2);

datePickerDialog1 = com.fourmob.datetimepicker.date.DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog2 = com.fourmob.datetimepicker.date.DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

date1.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
       datePickerDialog1.setYearRange(1910, 2037);
       datePickerDialog1.show(getSupportFragmentManager(), DATE_PICKER_1);
       datePickerDialog1.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {

         @Override
         public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
                date1.setText(" " + month + " " + day + " " + year);
                 }
              });
            }
         });

date2.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
      datePickerDialog2.setYearRange(1910, 2037);
      datePickerDialog2.show(getSupportFragmentManager(), DATE_PICKER_2);
      datePickerDialog2.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {

         @Override
         public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
                date2.setText(" " + month + " " + day + " " + year);
                  }
               });
             }
          });         

if(savedInstanceState != null) {
  DatePickerDialog datePickerDialog1= (DatePickerDialog) getSupportFragmentManager().findFragmentByTag(DATE_PICKER_1);
    if(datePickerDialog1!= null || datePickerDialog1.isAdded()) {
       datePickerDialog1.setOnDateSetListener(this);
    }

  DatePickerDialog datePickerDialog2 = (DatePickerDialog) getSupportFragmentManager().findFragmentByTag(DATE_PICKER_2);
    if(datePickerDialog2 != null || datePickerDialog2.isAdded()) {
        datePickerDialog2 .setOnDateSetListener(this);
    }
 }

1 个答案:

答案 0 :(得分:1)

在onCreate声明DatePickerDialog

之前

 DatePickerDialog datePickerDialog1;
 DatePickerDialog datePickerDialog2;

然后

    date1 = (EditText) findViewById(R.id.date1);
    date2 = (EditText) findViewById(R.id.date2);

    datePickerDialog1 = com.fourmob.datetimepicker.date.DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    datePickerDialog2 = com.fourmob.datetimepicker.date.DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    date1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                datePickerDialog1.setYearRange(1910, 2037);
                datePickerDialog1.show(getSupportFragmentManager(), DATE_PICKER_1);
                datePickerDialog1.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
                        date1.setText(" " + month + " " + day + " " + year);
                    }
                });
            }
        });

     date2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                datePickerDialog2.setYearRange(1910, 2037);
                datePickerDialog2.show(getSupportFragmentManager(), DATE_PICKER_2);
                datePickerDialog2.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
                        date2.setText(" " + month + " " + day + " " + year);
                    }
                });
            }
        });


          if(savedInstanceState != null) {

                        if(datePickerDialog1!= null || datePickerDialog1.isAdded()) {
                            datePickerDialog1.setOnDateSetListener(this);
                        }


                        if(datePickerDialog2 != null || datePickerDialog2 .isAdded()) {
                            datePickerDialog2 .setOnDateSetListener(this);
                        }
                }