gpt4 book ai didi

java - 月份数组抛出 ArrayIndexOutOfBoundException java

转载 作者:行者123 更新时间:2023-12-01 16:34:05 24 4
gpt4 key购买 nike

在我的回历应用程序中,如果我单击下个月按钮并到达年底(即最后一个月),或者如果我单击上个月按钮直到到达年初(即第一个月),应用程序崩溃并抛出 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);

位于 setGridCellAdapterToDate 方法中的 getHMonthAsString(hmonth)

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

我哪里出错了?

最佳答案

数组是从 0 开始的。如果月份是 12(如您在 onClick 中设置的那样),则您已超出数组末尾。

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

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

<小时/>

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

关于java - 月份数组抛出 ArrayIndexOutOfBoundException java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com