android NumberPicker 數組越界的坑

來源:互聯網
上載者:User

標籤:

被這個問題耽誤了一個多小時。。。

直接上解決方案,參考紅色部分。

private void initViews() {        wheel = (NumberPicker) findViewById(R.id.info_wheel_province);        wheelCity = (NumberPicker) findViewById(R.id.info_wheel_city);        wheelCity.setWrapSelectorWheel(false);        //讀取地址資訊json        final List<CityBean> cityBeans = ResUtils.initCityList(this);        final String[] pros = new String[cityBeans.size()];        for (int i = 0; i < pros.length; i++) {            pros[i] = cityBeans.get(i).getProvince();        }//        wheel.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//        wheelCity.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);        wheel.setMaxValue(pros.length - 1);        wheel.setMinValue(0);        wheel.setWrapSelectorWheel(false);        wheel.setDisplayedValues(pros);        wheel.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {            @Override            public void onValueChange(NumberPicker picker, int oldVal, final int newVal) {                String citys = cityBeans.get(newVal).getCitys();                final String[] split = citys.split(",");                wheelCity.setMinValue(0);                wheelCity.setValue(0);                if (split.length - 1 > wheelCity.getMaxValue()) {                    wheelCity.setDisplayedValues(split);                    wheelCity.setMaxValue(split.length - 1);                } else {                    wheelCity.setMaxValue(split.length - 1);                    wheelCity.setDisplayedValues(split);                }                wheelCity.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {                    @Override                    public void onValueChange(NumberPicker picker2, int oldVal2, int newVal2) {                        Toast.makeText(InfoActivity.this, pros[newVal] + " " + split[newVal2], Toast.LENGTH_LONG).show();                    }                });            }        });    }

有興趣的可以研究一下源碼找下原因。

android NumberPicker 數組越界的坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.