2014-01-16 86 views
0

你好我有我的android應用程序的問題,當我開始應用程序崩潰時,我嘗試添加一個顏色到一個Paint陣列,我是新編程,提前對不起,如果這是愚蠢的東西,這裏是崩潰的應用程序和日誌之類的代碼Android應用程序崩潰問題(初級程序員)

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

pps數組的長度爲55個元素(ID爲0到54),但在您的inisializer()方法中,您正在遍歷具有0到55的ID的元素。要解決此問題,您需要遍歷ids 0到54,因此請更改。 ..

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

​​

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++) { 

您正在嘗試存取權限的項目NR 55.可惜的是有NR 55沒有項目有55個項目從0開始。

+0

我可以同意答案的前半部分 - 沒有什麼可以阻止你在數組中從上到下導航。 –

+0

<=意味着55被允許。所以在你的初始化時你可以調用pps [i] = paint;這就是說pps [55] =繪畫; –

+0

謝謝大家,對於發佈這麼愚蠢的問題感到抱歉:/,我是編程新手,我從互聯網瞭解到的全部是 – user3115