简体   繁体   English

Android timepicker java.lang.ArrayIndexOutOfBoundsException 错误

[英]Android timepicker java.lang.ArrayIndexOutOfBoundsException error

I have a timepicker wherein我有一个时间选择器,其中
if hour is 0 it should display as with interval of 5如果小时为 0,则应显示为间隔为 5
["05", "10", "15" -> until "55"] ["05", "10", "15" -> 直到 "55"]
and if hour is NOT 0如果小时不是 0
["0", "05", -> "55"] [“0”,“05”,->“55”]

i'll post some of the codes here我会在这里发布一些代码

tp.setHour(0);
tp.setIs24HourView(true);
setMinuteIntervalWithOutZero(timepicker);
tp.setOnTimeChangedListener(mChangeTimeListener);
//set default time on load to 5 minutes
tp.setMinute(1);

onTimeChanged onTimeChanged

private TimePicker.OnTimeChangedListener mChangeTimeListener = new TimePicker.OnTimeChangedListener() {
        public void onTimeChanged(TimePicker tp, int selectedHour, int selectedMinute) {
            hour = selectedHour;
            minute = selectedMinute;

            try{
                if ( hour != 0 ){
                    setMinuteInterval(tp);
                } else {
                    setMinuteIntervalWithOutZero(tp);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }

        }
    };

setting Interval (5) and it will display 0-55, if hour is 1-23设置间隔(5),如果小时为1-23,它将显示0-55

private void setMinuteInterval(TimePicker tp) {
        try {
            minutePicker = (NumberPicker) tp.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
            minutePicker.setMinValue(0);
            minutePicker.setMaxValue((60 / INTERVAL));
            List<String> displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

setting interval (5) and will only display 5 - 55 if hour is 0设置间隔 (5),如果小时为 0,则仅显示 5 - 55

 private void setMinuteIntervalWithOutZero(TimePicker timePicker) {
        try {
            minutePicker = (NumberPicker) timePicker.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
            minutePicker.setMinValue(1);
            minutePicker.setMaxValue((60 / INTERVAL) - 1);
            List<String> displayedValues = new ArrayList<String>();
            for (int i = 5; i < 60; i += INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            minutePicker.setDisplayedValues(displayedValues.toArray(new String[0]));

} catch (Exception e) {
            e.printStackTrace();
        }
    }

if I swipe down the minute until the hour will display as 23(Hour is 0), it will crash and the error will be ArrayIndexOutOfBoundsException如果我向下滑动分钟直到小时将显示为 23(小时为 0),它将崩溃并且错误将是ArrayIndexOutOfBoundsException

could you help me ??你可以帮帮我吗 ??

In setMinuteInterval() you have this:setMinuteInterval()你有这个:

minutePicker.setMaxValue((60 / INTERVAL));

but you need this:但你需要这个:

minutePicker.setMaxValue((60 / INTERVAL) - 1);

because the values (min to max) need to be 0 to 11 (not 0 to 12)因为值(最小值到最大值)需要为 0 到 11(不是 0 到 12)

The crash occurs when you call setDisplayedValues() because you aren't passing enough display values to cover the range min to max.当您调用setDisplayedValues()时会发生崩溃,因为您没有传递足够的显示值来覆盖从 min 到 max 的范围。 The documentation for setDisplayedValues() says: setDisplayedValues()的文档说:

The displayed values.显示的值。 Note: The length of the displayed values array must be equal to the range of selectable numbers which is equal to getMaxValue() - getMinValue() + 1.注意:显示值数组的长度必须等于可选数字的范围,即 getMaxValue() - getMinValue() + 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM