在我的Hijri日历应用程序中,如果我单击下个月的按钮并到达年底(即最后一个月),或者单击上个月的按钮直到达到年初(即第一个月) ,应用程序崩溃并抛出java.lang.ArrayIndexOutOfBoundsException

    private ImageView calendarToJournalButton;
    private Button selectedDayMonthYearButton;
    private final String[] hmonths = {"Muharram", "Safar", "Rabi al-Awwal", "Rabi al-Akhir", "Jamadi al-Awwal", "Jamadi al-Akhir", "Rajab", "Shabaan", "Ramadhan", "Shawwal", "Zilqad", "Zilhajj"};
    private Button currentMonth;
    private ImageView prevMonth;
    private ImageView nextMonth;
    private GridView calendarView;
    private GridCellAdapter adapter;
    private Calendar _calendar;
    private int month, year, hmonth, hyear;
    private final DateFormat dateFormatter = new DateFormat();
    private static final String dateTemplate = "MMMM yyyy";
    private String hmonthname;
    private HijriCalendar hijri;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.simple_calendar_view);

            _calendar = Calendar.getInstance(Locale.getDefault());
            hijri = new HijriCalendar(_calendar.get(Calendar.YEAR),_calendar.get(Calendar.MONTH),_calendar.get(Calendar.DAY_OF_MONTH));

            month = _calendar.get(Calendar.MONTH) + 1;
            year = _calendar.get(Calendar.YEAR);

            hmonth = hijri.getHijriMonth();
            hmonthname = hijri.getHijriMonthName();
            hyear = hijri.getHijriYear();

            Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: " + year);
            Log.d(tag, "Islamic Calendar Instance:= " + "Month: " + hmonth + " " + "Year: " + hyear);

            selectedDayMonthYearButton = (Button) this.findViewById(R.id.selectedDayMonthYear);
            selectedDayMonthYearButton.setText("Selected: ");

            prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
            prevMonth.setOnClickListener(this);

            currentMonth = (Button) this.findViewById(R.id.currentMonth);
            currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()) + " | " + hmonthname + " " + hyear);

            nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
            nextMonth.setOnClickListener(this);

            calendarView = (GridView) this.findViewById(R.id.calendar);

            // Initialised
            adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, month, year, hmonth, hyear);
            adapter.notifyDataSetChanged();
            calendarView.setAdapter(adapter);
        }


    /**
     *
     * @param month
     * @param year
     */
    private void setGridCellAdapterToDate(int month, int year, int hmonth, int hyear)
        {
            adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, month, year, hmonth, hyear);
            _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
            currentMonth.setText(dateFormatter.format(dateTemplate, _calendar.getTime())  + " | " + getHMonthAsString(hmonth) + " " + hyear);
            adapter.notifyDataSetChanged();
            calendarView.setAdapter(adapter);
        }

    //Hijri Month
    private String getHMonthAsString(int i)
        {
            return hmonths[i];
        }


    @Override
    public void onClick(View v)
        {
            if (v == prevMonth)
                {
                    if (month <= 1)
                        {
                            month = 12;
                            year--;
                        }
                    else
                        {
                            month--;
                        }
                    if (hmonth <= 1)
                    {
                        hmonth = 12;
                        hyear--;
                    }
                    else
                    {
                        hmonth--;
                    }
                    Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: " + month + " Year: " + year);
                    Log.d(tag, "Setting Prev Islamic Month in GridCellAdapter: " + "Month: " + hmonth + " Year: " + hyear);
                    setGridCellAdapterToDate(month, year, hmonth, hyear);
                }
            if (v == nextMonth)
                {
                    if (month > 11)
                        {
                            month = 1;
                            year++;
                        }
                    else
                        {
                            month++;
                        }
                    if (hmonth > 11)
                    {

                        hmonth = 1;
                        hyear++;
                    }
                    else
                    {
                        hmonth++;
                    }
                    Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: " + month + " Year: " + year);
                    Log.d(tag, "Setting Next Islamic Month in GridCellAdapter: " + "Month: " + hmonth + " Year: " + hyear);
                    setGridCellAdapterToDate(month, year, hmonth, hyear);

                }

        }


logcat在此行显示错误:

currentMonth.setText(dateFormatter.format(dateTemplate, _calendar.getTime())  + " | " + getHMonthAsString(hmonth) + " " + hyear);


getHMonthAsString(hmonth)方法中的setGridCellAdapterToDate处。

如果我将getHMonthAsString(hmonth)替换为hmonth,则日历将正确地循环显示年份和月份,并且可以正常工作而不会出现任何错误,但是日历不会显示月份名称,而只会显示数字。

我要去哪里错了?

最佳答案

数组基于0。如果month是12(如在onClick中设置的),则表示数组结束。

public void onClick(View v) {
    if (v == prevMonth) {
        if (month <= 1) {
            month = 12; // Ouch.




另外,虽然我意识到源格式在很大程度上是一个宗教问题,尤其是在发布IMO时,最好减少垂直和水平空白。单个块的两个缩进级别似乎过多。反过来的代码并不是它有多棒的图表;)



编辑者:请注意您要添加和删除的信息。

07-25 23:46