gpt4 book ai didi

java - 滚动值时 Numberpicker 抛出 ArrayIndexOutOfBoundsException

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:46 27 4
gpt4 key购买 nike

嘿,我有一个简单的 NumberPicker,它有 15 个值 (100,200,300,400,...,1500)。当我滚动浏览数字时,数字选择器在我到达数字 1400(位置 13)时抛出 ArrayIndexOutOfBoundsException。请参阅下面的日志。

但我真的不知道。这似乎是 numberpicker 类的错误。

这是我的代码:

 tv_radius.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(CostLogActivity2.this);
dialog.setContentView(R.layout.radius_picker);
dialog.setTitle("Radius");

NumberPicker np_radius = (NumberPicker) dialog.findViewById(R.id.np_radius);

np_radius.setMaxValue(100); // max value 100
np_radius.setMinValue(1); // min value 1
np_radius.setWrapSelectorWheel(false);

String[] displayedValues = new String[15];
int x=0;
for(int i=0; i<displayedValues.length; i++){
x = x+100;
displayedValues[i] = Integer.toString(x);

}

np_radius.setDisplayedValues(displayedValues);

np_radius.setOnValueChangedListener(new OnValueChangeListener() {

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// TODO Auto-generated method stub

}
});

dialog.show();

}

});

错误日志:

04-07 13:55:33.383: E/AndroidRuntime(20233): FATAL EXCEPTION: main
04-07 13:55:33.383: E/AndroidRuntime(20233): java.lang.ArrayIndexOutOfBoundsException: length=15; index=15
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1774)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.widget.NumberPicker.incrementSelectorIndices(NumberPicker.java:1739)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.widget.NumberPicker.scrollBy(NumberPicker.java:1008)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.widget.NumberPicker.computeScroll(NumberPicker.java:964)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.drawChild(ViewGroup.java:2729)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.View.getDisplayList(View.java:10475)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.View.getDisplayList(View.java:10475)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.View.getDisplayList(View.java:10475)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.View.getDisplayList(View.java:10475)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.drawChild(ViewGroup.java:2850)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.View.draw(View.java:11041)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.widget.FrameLayout.draw(FrameLayout.java:450)
04-07 13:55:33.383: E/AndroidRuntime(20233): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2151)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.View.getDisplayList(View.java:10477)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:873)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewRootImpl.draw(ViewRootImpl.java:1914)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1638)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.os.Looper.loop(Looper.java:137)
04-07 13:55:33.383: E/AndroidRuntime(20233): at android.app.ActivityThread.main(ActivityThread.java:4464)
04-07 13:55:33.383: E/AndroidRuntime(20233): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 13:55:33.383: E/AndroidRuntime(20233): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 13:55:33.383: E/AndroidRuntime(20233): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
04-07 13:55:33.383: E/AndroidRuntime(20233): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
04-07 13:55:33.383: E/AndroidRuntime(20233): at dalvik.system.NativeStart.main(Native Method)

有人知道怎么解决吗?

最佳答案

这不是错误。来自docs :

显示值数组的长度必须等于可选数字的范围,即等于 getMaxValue() - getMinValue() + 1

您的 maxValue 是 100,minValue 是 1。因此,只需改用它:

numberPicker.setMaxValue(14);
numberPicker.setMinValue(0);

你也可以看看here

关于java - 滚动值时 Numberpicker 抛出 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911819/

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