拖放-java.lang.ArrayIndexOutOfBoundsException
我对android(和Java)是一个完全的新手,我从一个拖放教程中得到了一些示例代码,供大家玩转并尝试理解。做得相当不错,但这段代码会生成java.lang.ArrayIndexOutOfBoundsException错误,并在应用程序启动时每秒强制关闭一次 我猜这可能是关于应用程序没有清除其资源的简单问题,如果有人知道代码的哪一部分导致了它以及为什么会这样,我们将不胜感激。 在1.5和2.2上都试过 Main.java:拖放-java.lang.ArrayIndexOutOfBoundsException,java,android,Java,Android,我对android(和Java)是一个完全的新手,我从一个拖放教程中得到了一些示例代码,供大家玩转并尝试理解。做得相当不错,但这段代码会生成java.lang.ArrayIndexOutOfBoundsException错误,并在应用程序启动时每秒强制关闭一次 我猜这可能是关于应用程序没有清除其资源的简单问题,如果有人知道代码的哪一部分导致了它以及为什么会这样,我们将不胜感激。 在1.5和2.2上都试过 Main.java: package eas.org; import android.ap
package eas.org;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// draw the view
setContentView(new DrawView(this));
}
}
DrawView.java:
package eas.org;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private int balID = 0; // variable to know what ball is being dragged
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
// setting the start point for the balls
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(context,R.drawable.bol_groen, point1);
colorballs[1] = new ColorBall(context,R.drawable.bol_rood, point2);
colorballs[2] = new ColorBall(context,R.drawable.bol_blauw, point3);
}
// the method that draws the balls
@Override protected void onDraw(Canvas canvas) {
//canvas.drawColor(0xFFCCCCCC); //if you want another background color
//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
for (ColorBall ball : colorballs) {
// check if inside the bounds of the ball (circle)
// get the center for the ball
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
// calculate the radius from the touch to the center of the ball
double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
// if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
if (radCircle < 23){
balID = ball.getID();
break;
}
// check all the bounds of the ball (square)
//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
// balID = ball.getID();
// break;
//}
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
}
感谢你对我的帮助 ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException意味着您试图访问数组中不存在的项。例如,您有一个长度为3的数组
private ColorBall[] colorballs = new ColorBall[3];
数组的索引从0开始,因此要获取第一个colorball,您需要colorballs[0]
,第二个将是colorballs[1]
,最后是colorballs[2]
如果您要求使用colorbolls[3]
,您应该获得execption,因为索引3超出了正确的范围,即0到2(包括0到2)
我不确定代码中的第112行是哪一行,但请查看该行,看看您是否试图使用不在0和2之间的数字访问阵列。尝试设置
公共静态int count=0
看看这是否有帮助。我似乎发现了问题,尽管我不太清楚为什么会发生
在通过后退按钮退出应用程序后,“count”变量显然没有重置,因此它使数组变得疯狂
现在,我更改了代码,以确保每次调用ColorBall()时计数不超过3(我当前的对象数)(临时解决方法):
我认为这应该是问题所在:colorballs[balID-1].setY(Y-25);这很奇怪。我会在那里添加一些日志语句来了解更多的情况,或者在那里放置一个断点并在调试模式下运行,以便您可以看到哪个球导致它崩溃。嗯,不,count必须设置为1,应用程序才能运行。但尝试更改它,错误仍然存在。
10-13 15:15:15.038: ERROR/AndroidRuntime(492): FATAL EXCEPTION: main
10-13 15:15:15.038: ERROR/AndroidRuntime(492): java.lang.ArrayIndexOutOfBoundsException
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at eas.org.DrawView.onTouchEvent(DrawView.java:112)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.view.View.dispatchTouchEvent(View.java:3766)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.os.Looper.loop(Looper.java:123)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at java.lang.reflect.Method.invoke(Method.java:521)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-13 15:15:15.038: ERROR/AndroidRuntime(492): at dalvik.system.NativeStart.main(Native Method)
private ColorBall[] colorballs = new ColorBall[3];
public ColorBall(Context context, int drawable) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
if (count > 3) count=1;
id=count;
count++;
}
public ColorBall(Context context, int drawable, Point point) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
img = BitmapFactory.decodeResource(context.getResources(), drawable);
if (count > 3) count=1;
id=count;
count++;
coordX= point.x;
coordY = point.y;
}