几个月阵列抛ArrayIndexOutOfBoundException的java [英] Months array throw ArrayIndexOutOfBoundException java
问题描述
在我回历的应用程序,如果我点击下个月按钮,到达直到今年年底(即最后一个月),或者如果我点击$ P $光伏月按钮,直到我达到年初(即第一个月),应用程序崩溃并引发 java.lang.ArrayIndexOutOfBoundsException
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);
}
}
在logcat中显示错误在这行:
The logcat shows the error at this line:
currentMonth.setText(dateFormatter.format(dateTemplate, _calendar.getTime()) + " | " + getHMonthAsString(hmonth) + " " + hyear);
在 getHMonthAsString(hmonth)
在 setGridCellAdapterToDate
方法。
如果我更换 getHMonthAsString(hmonth)
与 hmonth
,日历正确循环的年月,并工作正常没有任何错误,但随后它不显示月份名称,只是这些数字。
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.
我在哪里去了?
推荐答案
数组是从零开始的。如果月份是12,当你在设置的onClick
,你过去的数组的末尾。
Arrays are 0-based. If month is 12, as you set in onClick
, you're past the end of your array.
public void onClick(View v) {
if (v == prevMonth) {
if (month <= 1) {
month = 12; // Ouch.
此外,当我意识到源格式主要是宗教的问题,特别是国际海事组织发布时,它的最大努力减少纵向和横向的空白。对于单块两个级别的缩进似乎过高。 code开启它的一边是不是它是如何真棒的曲线)
Also, while I realizing source formatting is largely a matter of religion, particularly when posting IMO it's best to reduce vertical and horizontal whitespace. Two levels of indentation for a single block seems excessive. Code turned on its side is not a graph of how awesome it is ;)
编辑:心系你添加和删除信息
Editors: be mindful of the information you're adding and removing.
这篇关于几个月阵列抛ArrayIndexOutOfBoundException的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!