关闭

NumberPicker中ArrayIndexOutOfBoundsException解决方案

标签: NumberPick越界ArrayIndex
586人阅读 评论(0) 收藏 举报
分类:
  • 场景
    通常使用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);
}
  • 以上两种方案亲测可以解决越界问题
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:128778次
    • 积分:2005
    • 等级:
    • 排名:第19315名
    • 原创:67篇
    • 转载:16篇
    • 译文:0篇
    • 评论:65条
    最新评论