大家好我的Android应用程序有问题,当我启动应用程序时它崩溃了,当我尝试向Paint数组添加颜色时,我是编程的新手,如果它是愚蠢的事情,请提前对不起,这是崩溃应用程序和日志的类的代码
01-17 00:12:40.242: E/Trace(29767): error opening trace file: No such file or directory (2)
01-17 00:12:40.302: D/AndroidRuntime(29767): Shutting down VM
01-17 00:12:40.302: W/dalvikvm(29767): threadid=1: thread exiting with uncaught exception (group=0x40da2318)
01-17 00:12:40.302: E/AndroidRuntime(29767): FATAL EXCEPTION: main
01-17 00:12:40.302: E/AndroidRuntime(29767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asdfghjk/com.example.asdfghjk.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=55; index=55
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.ActivityThread.access$600(ActivityThread.java:134)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.os.Looper.loop(Looper.java:137)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.ActivityThread.main(ActivityThread.java:4744)
01-17 00:12:40.302: E/AndroidRuntime(29767): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 00:12:40.302: E/AndroidRuntime(29767): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 00:12:40.302: E/AndroidRuntime(29767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-17 00:12:40.302: E/AndroidRuntime(29767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-17 00:12:40.302: E/AndroidRuntime(29767): at dalvik.system.NativeStart.main(Native Method)
01-17 00:12:40.302: E/AndroidRuntime(29767): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=55; index=55
01-17 00:12:40.302: E/AndroidRuntime(29767): at com.example.asdfghjk.MyBall.inisializer(MyBall.java:57)
01-17 00:12:40.302: E/AndroidRuntime(29767): at com.example.asdfghjk.MyBall.<init>(MyBall.java:22)
01-17 00:12:40.302: E/AndroidRuntime(29767): at com.example.asdfghjk.GameView.<init>(GameView.java:25)
01-17 00:12:40.302: E/AndroidRuntime(29767): at com.example.asdfghjk.MainActivity.onCreate(MainActivity.java:17)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.Activity.performCreate(Activity.java:5008)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-17 00:12:40.302: E/AndroidRuntime(29767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
01-17 00:12:40.302: E/AndroidRuntime(29767): ... 11 more
代码:
public class MyBall {
private GameView gameV;
Random dice = new Random();
int x, y, xSpeed, ySpeed, rad;
Paint paint = new Paint();
Paint pps[] = new Paint[55];
List<Paint> paints = new ArrayList<Paint>();
public MyBall(GameView gameV) {
this.gameV = gameV;
rad = 55;
inisializer();
// set color of circles
}
public void onDraw(Canvas canvas) {
// draw circles with the color thats setted in initialiser
for (int s = rad; s >= 0; s--) {
canvas.drawCircle(x, y, s, pps[s]);
}
update();
}
private void update() {
if (x > gameV.getWidth() - rad - xSpeed || x - rad < 0) {
xSpeed = -xSpeed;
}
if (y > gameV.getHeight() - rad || y - rad < 0) {
ySpeed = -ySpeed;
}
x += xSpeed;
y += ySpeed;
}
private void inisializer() {
for (int i = 0; i <= rad; i++) {
paint.setARGB(250, dice.nextInt(255), dice.nextInt(255),
dice.nextInt(255));
pps[i] = paint;
}
x = 250;
y = 250;
xSpeed = dice.nextInt(3);
ySpeed = 1;
}
}
答案 0 :(得分:1)
第一
for (int s = rad; s >= 0; s--) {
应该是:
for (int s = rad-1; s >= 0; s--) {
第二
for (int i = 0; i <= rad; i++) {
应该是:
for (int i = 0; i < rad; i++) {
您正在尝试访问项目编号55.不幸的是,没有项目编号55.从0开始有55项。
答案 1 :(得分:0)
您的pps
数组长度为55个元素(ID为0到54),但在inisializer()
方法中,您正在使用ID 0到55迭代元素。要修复此问题,您需要想要迭代ids 0到54,所以改变......
for (int i = 0; i <= rad; i++) {
到
for (int i = 0; i < rad; i++) {