In my Hijri calendar app, if I click on the next month button and reach till the end of year (i.e. the last month) or if I click on the prev Month button till I reach the beginning of the year (i.e. the first month), the app crashes and throws 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);
}
}
The logcat shows the error at this line:
currentMonth.setText(dateFormatter.format(dateTemplate, _calendar.getTime()) + " | " + getHMonthAsString(hmonth) + " " + hyear);
at getHMonthAsString(hmonth)
in setGridCellAdapterToDate
method.
If I replace getHMonthAsString(hmonth)
with hmonth
, the calendar loops the years and months correctly and works fine without any errors, but then it does not display the month names, just the numbers.
Where am I going wrong?