嘿我不知道为什么但是这个循环不起作用并且每次都给我同样的错误。如果您知道修复程序,请回答它。我无法解决这个问题。
这是我的循环
double angle = 0;
for (int i = 0; i < 120; i++, angle += 3) {
int x = (int) Math.ceil(i * 8.5);
int t = ((byte) (-Math.abs(bytes[x]) + 128))
* (canvas.getHeight() / 4) / 128;
points[i * 4] = (float) (getWidth() / 2
+ radius
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 1] = (float) (getHeight() / 2
+ radius
* Math.sin(Math.toRadians(angle)));
points[i * 4 + 2] = (float) (getWidth() / 2
+ (radius + t)
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 3] = (float) (getHeight() / 2
+ (radius + t)
* Math.sin(Math.toRadians(angle)));
}
这是我的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.androidgrid.faysr, PID: 19297
java.lang.ArrayIndexOutOfBoundsException: length=128; index=128
at app.androidgrid.faysr.visualizer.view.CircleBarVisualizer.onDraw(CircleBarVisualizer.java:94)
at android.view.View.draw(View.java:17096)
at android.view.View.updateDisplayListIfDirty(View.java:16078)
我也尝试将 120 更改为 128 和 i&lt; 120 到 i&lt; = 120 ,但它不是
这是我的java类
public class CircleBarVisualizer extends BaseVisualizer {
private float[] points;
private Paint circlePaint;
private int radius;
public CircleBarVisualizer(Context context) {
super(context);
}
public CircleBarVisualizer(Context context,
@Nullable AttributeSet attrs) {
super(context, attrs);
}
public CircleBarVisualizer(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void init() {
paint.setStyle(Paint.Style.STROKE);
circlePaint = new Paint();
radius = -1;
}
@Override
protected void onDraw(Canvas canvas) {
if (radius == -1) {
radius = getHeight() < getWidth() ? getHeight() : getWidth();
radius = (int) (radius * 0.65 / 2);
double circumference = 2 * Math.PI * radius;
paint.setStrokeWidth((float) (circumference / 120));
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(4);
}
circlePaint.setColor(color);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, circlePaint);
if (bytes != null) {
if (points == null || points.length < bytes.length * 4) {
points = new float[bytes.length * 4];
}
double angle = 0;
try {
for (int i = 0; i < 120; i++, angle += 3) {
int x = (int) Math.ceil(i * 8.5);
int t = ((byte) (-Math.abs(bytes[x]) + 128))
* (canvas.getHeight() / 4) / 128;
points[i * 4] = (float) (getWidth() / 2
+ radius
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 1] = (float) (getHeight() / 2
+ radius
* Math.sin(Math.toRadians(angle)));
points[i * 4 + 2] = (float) (getWidth() / 2
+ (radius + t)
* Math.cos(Math.toRadians(angle)));
points[i * 4 + 3] = (float) (getHeight() / 2
+ (radius + t)
* Math.sin(Math.toRadians(angle)));
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
canvas.drawLines(points, paint);
}
super.onDraw(canvas);
}}
我也试试看,所以它不会让应用程序崩溃,但结果并不像我说的那样。
答案 0 :(得分:1)
java.lang.ArrayIndexOutOfBoundsException: length=128; index=128
- &GT;这意味着您正在尝试访问仅包含128个元素的数组的第129个元素。
请记住,在Java数组中,基于0,因此您的案例中的有效索引从0到127
不等答案 1 :(得分:1)
对于i = 0 ... 120
,在循环的最后一次迭代中使用以下语句:
points[i * 4 + 3]
将尝试访问仅包含128个元素的数组((119 * 4) + 3 = 479
)的第479个元素。
为了填充您的数组,您需要i < 31
,因为(31 * 4) + 3 = 127
。
答案 2 :(得分:0)
即使您有<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.ScriptTask" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-14.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.ManagedDTS" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-14.0.0.0" newVersion="14.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.DTSRuntimeWrap" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-14.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.DTSPipelineWrap" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-14.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.SqlServer.PipelineHost" publicKeyToken="89845dcd8080cc91" culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0-14.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
循环,它会将索引for
迭代到0
,但在您的120
循环内,您尝试访问不同的索引({{1} },for
等),导致实际问题。
想象一下,你有i * 4
大小的i * 4 +
数组。现在,在第一次迭代时,points
将是2
,但您将尝试访问元素i
,这相当于0
。但是, i * 4 + 3
数组大小为2.
因为,您正在尝试访问元素3
(在每次迭代中都会更高端)。我建议将points
循环更改为避免i * 4 + 3
(如果数组大小为for
)
ArrayIndexOutOfBoundsException
答案 3 :(得分:-1)
你必须检查&#34;点&#34;的大小。阵列。考虑到您的代码,其大小应至少为(119 * 4 + 3),即479。
&#34;点&#34;数组大小为4 *&#34;字节&#34;数组大小,但你的代码没有显示&#34;字节&#34;数组大小;可能是祖先类中定义的东西。