嘿我有一个简单的NumberPicker,有15个值(100,200,300,400,...,1500)。 当我滚动数字时,当我到达数字1400(位置13)时,numberpicker会抛出一个ArrayIndexOutOfBoundsException。见下面的日志。
但我真的不知道。这似乎是numberpicker类的一个bug。
这是我的代码:
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)
有谁知道如何修复它?
答案 0 :(得分:8)
这不是一个错误。来自docs:
显示值数组的长度必须等于可选数字的范围,等于getMaxValue() - getMinValue() + 1
。
maxValue
为100,minValue
为1.所以,请改用它:
numberPicker.setMaxValue(14);
numberPicker.setMinValue(0);
您还可以查看here
答案 1 :(得分:1)
您的问题的Android文档是here。
问题在于这段代码np_radius.setMaxValue(100)
。您应该将此行替换为np_radius.setMaxValue(14)
。