gpt4 book ai didi

android, NumberPicker 越界

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:29 25 4
gpt4 key购买 nike

NumberPicker serviceWheel = (NumberPicker) findViewById(R.id.serviceSelector);
serviceWheel.setMaxValue(serviceArray.length());
String[] profiles = new String[serviceArray.length()];
for (int i = 0; i < serviceArray.length(); i++){
JSONObject profileObject = serviceArray.getJSONObject(i);
profiles[i] = profileObject.getString("description");
}
serviceWheel.setDisplayedValues(profiles);
serviceWheel.setWrapSelectorWheel(false);


 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1756)
at android.widget.NumberPicker.incrementSelectorIndices(NumberPicker.java:1722)
at android.widget.NumberPicker.scrollBy(NumberPicker.java:1097)
at android.widget.NumberPicker.onTouchEvent(NumberPicker.java:880)
at android.view.View.dispatchTouchEvent(View.java:7565)

serviceArray 是一个JSONArray,长度为3。我试图在实例化数组时添加填充 ( serviceArray.length() + 1 ) 但这导致在显示 Activity 之前失败。 NumberPicker 显示正常,但是当我滚动到数组的最后一个条目时,它因此错误而崩溃。我的代码中没有引用代码行

最佳答案

由此改变:

serviceWheel.setMaxValue(serviceArray.length());

为此:

serviceWheel.setMaxValue(serviceArray.length() - 1);

关于android, NumberPicker 越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078633/

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