user3115
user3115

Reputation: 23

Issue with Android app crashing (Beginner Programmer)

hello guys i have a problem with my android application , when i start the app it crashes at the point when i try to add a color to a Paint array , i am new to programming , sorry in advance if it is something stupid , here is the code of the class that crashes the app and the log

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

Code:

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;
}

}

Upvotes: 0

Views: 90

Answers (2)

Robin Dijkhof
Robin Dijkhof

Reputation: 19298

First

for (int s = rad; s >= 0; s--) {

should be:

for (int s = rad-1; s >= 0; s--) {

Second

for (int i = 0; i <= rad; i++) {

Should be:

for (int i = 0; i < rad; i++) {

You are trying to acces item nr 55. Unfortunatelly there is no item nr 55. There are 55 items starting from 0.

Upvotes: 1

Yjay
Yjay

Reputation: 2737

Your pps array is 55 elements long (with ids 0 through 54), but in your inisializer() method, you are iterating through elements with ids 0 through 55. To fix this, you'll want to iterate through ids 0 to 54, so change...

for (int i = 0; i <= rad; i++) {

to

for (int i = 0; i < rad; i++) {

Upvotes: 0

Related Questions