在我的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 :(得分:6)
数组是从0开始的。如果您在onClick
中设置月份为12,则表示您已经超过了数组的结尾。
public void onClick(View v) {
if (v == prevMonth) {
if (month <= 1) {
month = 12; // Ouch.
此外,虽然我认识到源格式化主要是宗教问题,特别是在发布IMO时,最好减少垂直和水平空白。单个块的两个级别的缩进似乎过多。代码转向它不是一个多么棒的图表;)
编辑:请注意您添加和删除的信息。