NumberPicker中ArrayIndexOutOfBoundsException解决方案

  • 场景
    通常使用NumberPicker我们都是只初始化一次数据就好了,但最近项目需要做一个城市选择器,每次选择了一个省份或者城市,都会去更新下城市或者区县。然而使用如下代码去更新NumberPicker数据时,会报数组越界的异常:
private void updateCitySelector() {
    cityPicker.setDisplayedValues(mCitys);
    cityPicker.setMaxValue(mCitys.length - 1);
}
java.lang.ArrayIndexOutOfBoundsException: length=1; index=3
at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1882)
at android.widget.NumberPicker.incrementSelectorIndices(NumberPicker.java:1848)
at android.widget.NumberPicker.scrollBy(NumberPicker.java:1142)
at android.widget.NumberPicker.computeScroll(NumberPicker.java:1097)
at android.view.View.draw(View.java:15984)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16111)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16204)
at android.view.View.draw(View.java:16113)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16111)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16111)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16204)
at android.view.View.draw(View.java:16113)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16204)
at android.view.View.draw(View.java:16113)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16111)
at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
at android.view.View.draw(View.java:16204)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

  • 解决方案一
    1、当前NumberPicker的最大值大于数组大小时,先setMaxValue再setDisplayedValues。
    2、当前NumberPicker的最大值小于数组大小时,先setDisplayedValues再setMaxValue。
private void updateCitySelector() {
    int oldMax = cityPicker.getMaxValue();
    int newMax = mCitys.length - 1;
    if(newMax > oldMax) {
        cityPicker.setDisplayedValues(mCitys);
        cityPicker.setMaxValue(newMax);
    } else {
        cityPicker.setMaxValue(newMax);
        cityPicker.setDisplayedValues(mCitys);
    }
}
  • 解决方案二(推荐):
    在设置最大值和最新数组数据前,先将数据设为null。
private void updateCitySelector() {
    cityPicker.setDisplayedValues(null);
    cityPicker.setMaxValue(mCitys.length - 1);
    cityPicker.setDisplayedValues(mCitys);
}
  • 以上两种方案亲测可以解决越界问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值