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

最佳答案

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

关于android - DatePickerDialog错误: fragment 已添加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40460859/

10-13 03:06