无效的希吉拉月日:31

Invalid Hijrah Day of Month : 31

我尝试创建一个简单的 activity,其中包含一个按钮和一个显示日期的文本视图。单击该按钮时应显示 HijriDatePicker 对话框。我尝试使用 ummalqura-calendar 和对话框来创建它,我尝试使用 HijriDatePicker 创建对话框。

但是,经过多次尝试,还是不行。有时 activity 会在我单击按钮时停止,有时它会崩溃。我已经尝试了很多失败的尝试。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 13658
    com.github.msarhan.ummalqura.calendar.DateTimeException: Invalid Hijrah day of month: 31
        at com.github.msarhan.ummalqura.calendar.HijrahChronology.getEpochDay(HijrahChronology.java:255)
        at com.github.msarhan.ummalqura.calendar.HijrahChronology.toGregorian(HijrahChronology.java:697)
        at com.github.msarhan.ummalqura.calendar.UmmalquraCalendar.set(UmmalquraCalendar.java:259)
        at net.alhazmy13.hijridatepicker.date.hijri.HijriDatePickerDialog.getEndDate(HijriDatePickerDialog.java:1028)
        at net.alhazmy13.hijridatepicker.date.hijri.MonthAdapter.getCount(MonthAdapter.java:146)
        at android.widget.ListView.setAdapter(ListView.java:493)
        at net.alhazmy13.hijridatepicker.date.hijri.DayPickerView.refreshAdapter(DayPickerView.java:142)
        at net.alhazmy13.hijridatepicker.date.hijri.DayPickerView.setController(DayPickerView.java:114)
        at net.alhazmy13.hijridatepicker.date.hijri.DayPickerView.<init>(DayPickerView.java:105)
        at net.alhazmy13.hijridatepicker.date.hijri.SimpleDayPickerView.<init>(SimpleDayPickerView.java:32)
        at net.alhazmy13.hijridatepicker.date.hijri.HijriDatePickerDialog.onCreateView(HijriDatePickerDialog.java:340)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

和我的主要 activity java

public class MainActivity extends AppCompatActivity implements HijriDatePickerDialog.OnDateSetListener{
    TextView tvDate;
    TextView etDate;
    HijriDatePickerDialog.OnDateSetListener setListener;

    public MainActivity() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvDate = findViewById(R.id.tv_date);
        etDate = findViewById(R.id.et_date);


        tvDate.setOnClickListener(v -> {
            UmmalquraCalendar now = new UmmalquraCalendar();
            HijriDatePickerDialog dpd = HijriDatePickerDialog.newInstance(
                    MainActivity.this,
                    now.get(Calendar.YEAR),
                    now.get(Calendar.MONTH),
                    now.get(Calendar.DAY_OF_MONTH)
            );
            dpd.setMinDate(new UmmalquraCalendar(1440,0,1));
            dpd.show(getSupportFragmentManager(), "Datepickerdialog");
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        assert getFragmentManager() != null;
        HijriDatePickerDialog dpd = (HijriDatePickerDialog) getSupportFragmentManager().findFragmentByTag("Datepickerdialog");
        if (dpd != null) dpd.setOnDateSetListener(this);
    }

    @Override
    public void onDateSet(HijriDatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        String date = "You picked the following date: " + dayOfMonth + "/" + (++monthOfYear) + "/" + year;
        System.out.println(date);
        etDate.setText(date);
    }
}

这个问题似乎是 msarhan-library 中的一个已知错误。其相关网站上也同时关闭了issue。这表明已在最新版本中应用了修复程序。

所以我建议您将您的 marhan 库更新到版本 v2.0.2,这可能会解决您的问题。

更新:

查看您使用的日期选择器库时,我们可以看到它仍然使用 version v2.0.1。您要么等到日期选择器库的作者修复依赖项,要么构建您自己的日期选择器库版本。