我设置了int[] ledPositionControl = new int[33]
。但它仍然说,我有它超出范围。我做了一个println,它只向我展示了32个,在我看来,这是正确的。
任何人都可以帮我解决这个问题吗?
logcat的:
07-31 12:10:32.421: E/AndroidRuntime(22858): FATAL EXCEPTION: main
07-31 12:10:32.421: E/AndroidRuntime(22858): java.lang.ArrayIndexOutOfBoundsException: length=33; index=33
07-31 12:10:32.421: E/AndroidRuntime(22858): at com.example.dotmatrixleddisplay.MySurfaceView.buttonPositionControl(MySurfaceView.java:100)
07-31 12:10:32.421: E/AndroidRuntime(22858): at com.example.dotmatrixleddisplay.MySurfaceView.surfaceChanged(MySurfaceView.java:74)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.SurfaceView.updateWindow(SurfaceView.java:623)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.SurfaceView.access$000(SurfaceView.java:86)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:178)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:703)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1942)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1108)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4491)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:755)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.Choreographer.doCallbacks(Choreographer.java:585)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.Choreographer.doFrame(Choreographer.java:555)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:741)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.os.Handler.handleCallback(Handler.java:615)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.os.Looper.loop(Looper.java:137)
07-31 12:10:32.421: E/AndroidRuntime(22858): at android.app.ActivityThread.main(ActivityThread.java:4949)
07-31 12:10:32.421: E/AndroidRuntime(22858): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 12:10:32.421: E/AndroidRuntime(22858): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 12:10:32.421: E/AndroidRuntime(22858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
07-31 12:10:32.421: E/AndroidRuntime(22858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
07-31 12:10:32.421: E/AndroidRuntime(22858): at dalvik.system.NativeStart.main(Native Method)
buttonPositionControl方法:
int offset;
int ledSizeControl;
int[] ledPositionControl = new int[33];
int ledBmpSize;
int positionControlSize;
final int MAX_CONTROL=64;
private void buttonPositionControl() {
//button position + led position
int dpi;
dpi=GraphicsMode.dpi;
if(maxWidth<=maxHeight){//port
if(maxWidth/dpi<2.6){
ledSizeControl=0;
}
else if(maxWidth/dpi<3.25){
ledSizeControl=4;
}
else{
ledSizeControl=8;
}
ledBmpSize = ledBmp[0+ledSizeControl].getWidth();
positionControlSize = ledBmpSize<<2;
for(int i=0;i<=MAX_CONTROL;i++){
ledPositionControl[i] = maxWidth/2+(i-MAX_CONTROL/2)*ledBmpSize;
System.out.println("LEDPOSITION = "+i);
}
buttonPositionY = resetPositionY =
ledPositionControl[MAX_CONTROL]+positionControlSize;
buttonPositionX = ledPositionControl[2];
resetPositionX = buttonPositionX+positionControlSize*5;
}
else{//if(maxWidth<=maxHeight){land
if(maxHeight/dpi<2.6){
ledSizeControl=0;
}
else if(maxHeight/dpi<3.25){
ledSizeControl=4;
}
else{
ledSizeControl=8;
}
ledBmpSize = ledBmp[0+ledSizeControl].getWidth();
positionControlSize = ledBmpSize<<2;
for(int i=0;i<=MAX_CONTROL;i++){
ledPositionControl[i] = maxHeight/2+(i-MAX_CONTROL/2)*ledBmpSize;
}
buttonPositionX = resetPositionX =
ledPositionControl[MAX_CONTROL]+positionControlSize;
buttonPositionY =ledPositionControl[2];
resetPositionY = buttonPositionY+positionControlSize*5;
}//if else
System.out.println("ledSizeControl:"+ledSizeControl);
System.out.println("ledBmpSize"+ledBmpSize+" maxWidth/dpi"+maxWidth/dpi);
}
答案 0 :(得分:4)
首先,你的循环应该在i
=数组长度之前结束。
for(int i=0;i<ledPositionControl.length;i++){
ledPositionControl[i] = maxWidth/2+(i-MAX_CONTROL/2)*ledBmpSize;
System.out.println("LEDPOSITION = "+i);
}
其次,循环后的行应为:
buttonPositionY = resetPositionY = ledPositionControl[ledPositionControl.length - 1]+positionControlSize;
不要忘记这些问题会在您的代码中出现两次。
答案 1 :(得分:1)
试试这个
更改此
for(int i=0;i<=MAX_CONTROL;i++)
到
for(int i=0;i<MAX_CONTROL;i++)
或
for(int i=1;i<=MAX_CONTROL;i++)
ledPositionControl[i - 1] = //your's
因为 MAX_CONTROL 值 33 如果您使用0开始 i ,我应该以0到32结束
答案 2 :(得分:1)
尝试这一点,如果它不让我知道它肯定会帮助你
Set MAX_CoNTROL = ledPositionControl.size();
for(int i=0;i<MAX_CONTROL;i++){
ledPositionControl[i] = maxHeight/2+(i-MAX_CONTROL/2)*ledBmpSize;
}
和Remove =登录for循环使你的循环转到索引,因为你知道数组从0索引开始所以最后一个索引将是32而不是33在日志中33指的是总的eumment计数阵列。
答案 3 :(得分:-1)
你正在迭代太多。将for循环中的&lt; =更改为直接&lt;你应该好。此外,您尝试访问ledPositionControl [MAX_CONTROL]。那也将失败,因为那是一个超过数组的最后一个元素。
for(int i=0;i<MAX_CONTROL;i++){
ledPositionControl[i] = maxWidth/2+(i-MAX_CONTROL/2)*ledBmpSize;
System.out.println("LEDPOSITION = "+i);
}
buttonPositionY = resetPositionY =
ledPositionControl[MAX_CONTROL-1]+positionControlSize;
buttonPositionX = ledPositionControl[2];
resetPositionX = buttonPositionX+positionControlSize*5;
我认为关键是你需要了解在一个分配有32项的数组中,你无法访问数组[32]。数组的最后一个元素是数组[31],因为数组是基于零的,这意味着它们从0开始,最多可达到ARRAY_SIZE-1的最大索引。