android - Months array throw ArrayIndexOutOfBoundException java -
in hijri calendar app, if click on next month button , reach till end of year (i.e. last month) or if click on prev month button till reach beginning of year (i.e. first month), app crashes , 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 activity 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 error @ line:
currentmonth.settext(dateformatter.format(datetemplate, _calendar.gettime()) + " | " + gethmonthasstring(hmonth) + " " + hyear);
at gethmonthasstring(hmonth)
in setgridcelladaptertodate
method.
if replace gethmonthasstring(hmonth)
hmonth
, calendar loops years , months correctly , works fine without errors, not display month names, numbers.
where going wrong?
arrays 0-based. if month 12, set in onclick
, you're past end of array.
public void onclick(view v) { if (v == prevmonth) { if (month <= 1) { month = 12; // ouch.
also, while realizing source formatting largely matter of religion, particularly when posting imo it's best reduce vertical , horizontal whitespace. 2 levels of indentation single block seems excessive. code turned on side not graph of how awesome ;)
editors: mindful of information you're adding , removing.
Comments
Post a Comment